Power BI - Count in power query across columns

rsj88

New Member
Joined
Feb 20, 2018
Messages
38
Hi,

I have the below dataset

RiskRisk 2Risk 3
HighMediumLow
MediumLowMedium

when i create a custom column at the end i need to rank it to show the highest value based on below

1.High,
2.Medium
3.Low

Result:

RiskRisk 2Risk 3Final
HighMEdiumLowHigh
MEdiumLowMediumLow

thanks in advance
RJ
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Why in your results is the second line Low, when obviously Medium is higher ranked.
 
Upvote 0
I have a workaround. It consists of replacing the High with 3, Medium with 2 and Low with 1. Then adding a column determining the max of the three columns. Here is the Mcode.

l
VBA Code:
et
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Risk", type text}, {"Risk 2", type text}, {"Risk 3", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","High","3",Replacer.ReplaceText,{"Risk", "Risk 2", "Risk 3"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Medium","2",Replacer.ReplaceText,{"Risk", "Risk 2", "Risk 3"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Low","1",Replacer.ReplaceText,{"Risk", "Risk 2", "Risk 3"}),
    #"Added Custom" = Table.AddColumn(#"Replaced Value2", "Custom", each List.Max({[Risk],[Risk 2],[Risk 3]}))
in
    #"Added Custom"
 
Upvote 0
another approach
RiskRisk 2Risk 3RiskRisk 2Risk 3Rank
HighMediumLowHighMediumLowHigh
MediumLowMediumMediumLowMediumMedium

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Rank = Table.AddColumn(Source, "Rank", each if List.Count(List.Select(Record.FieldValues(_), each _ = "High")) >= 1 then "High" else if List.Count(List.Select(Record.FieldValues(_), each _ = "Medium")) >= 2 then "Medium" else if List.Count(List.Select(Record.FieldValues(_), each _ = "Low")) >= 3 then "Low" else "Low")
in
    Rank
 
Last edited:
Upvote 0
or even
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Rank = Table.AddColumn(Source, "Rank", each if List.Count(List.Select(Record.FieldValues(_), each _ = "High")) >= 1 then "High" else if List.Count(List.Select(Record.FieldValues(_), each _ = "Medium")) >= 2 then "Medium" else "Low")
in
    Rank
RiskRisk 2Risk 3RiskRisk 2Risk 3Rank
HighHighHighHighHighHighHigh
HighHighMediumHighHighMediumHigh
HighMediumMediumHighMediumMediumHigh
HighMediumLowHighMediumLowHigh
HighLowLowHighLowLowHigh
MediumMediumMediumMediumMediumMediumMedium
MediumMediumLowMediumMediumLowMedium
MediumLowLowMediumLowLowLow
LowLowLowLowLowLowLow
 
Last edited:
Upvote 0
Hi,

if i wanted this rank for for the last 2 columns of the dataset eg: Risk2 and Risk3. how would be amend the code?

Thanks
 
Upvote 0
Ok lets change the logic

I have changed high low medium to
High=1,
Medium=2,
Low=3

Risk1Risk2Risk3
123
131

would it be easier to get the min number of the last 2 columns in each row?

Thanks
 
Upvote 0
criteria are exactly the same as before
but try custom column: if List.Count(List.Select(List.LastN(Record.FieldValues(_),2), each _ = "High")) >= 1 then "High" else if List.Count(List.Select(List.LastN(Record.FieldValues(_),2), each _ = "Medium")) >= 1 then "Medium" else "Low"
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top