Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
vincentakatoh
Helper IV
Helper IV

Power Query Conditional columns

Hi, 

Trying to figure out the M code equivalent to add column,"final test result", in power query, 

 

- In column "s/n", look for equivalent s/n

- In column "station", look for equivalent station, 

- In column "test count", look for test count, 

 

output

- if "test count" is not highest, output =null

- if "test count" is higest, output= test result. 

 

Below is a sample data (actual data >1m rows)

2017-06-15 00_13_29-Book1 - Excel.png

 

1 ACCEPTED SOLUTION

You can use Group By:

 

Group By 3.png

 

let
    Source = Table1,
    #"Grouped Rows" = Table.Group(Source, {"Srudent", "subject"}, {{"LastAttempt", each List.Max([Attempts]), type number}, {"AllData", each _, type table}}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Attempts", "test result"}, {"Attempts", "test result"}),
    #"Added Custom" = Table.AddColumn(#"Expanded AllData", "Final Test Result", each if [Attempts] = [LastAttempt] then [test result] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"LastAttempt"})
in
    #"Removed Columns"
Specializing in Power Query Formula Language (M)

View solution in original post

3 REPLIES 3
EvertonPessoa
Helper I
Helper I

Hi @vincentakatoh,

 

I did not understand the comparison value, it follows Power M:

 

Table.AddColumn(last Steps , "Name Column", each if [Test Count] >  "?" then [test result] else "null")

Hi @EvertonPessoa

Thanks. Will try another example as below:

 

I can get the "First test results quickly, by using below

"First Test Result", each if [Attempts] = 1 then [test result] else null 

 

Nonetheless, struggling to add column "Final test result"

The intend is to get the final (last attempt) test result for each "student" and "subject". There is no limit on number of attempts for each subject. 

 

2017-06-15 08_37_10-.png

 

 

 

You can use Group By:

 

Group By 3.png

 

let
    Source = Table1,
    #"Grouped Rows" = Table.Group(Source, {"Srudent", "subject"}, {{"LastAttempt", each List.Max([Attempts]), type number}, {"AllData", each _, type table}}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Attempts", "test result"}, {"Attempts", "test result"}),
    #"Added Custom" = Table.AddColumn(#"Expanded AllData", "Final Test Result", each if [Attempts] = [LastAttempt] then [test result] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"LastAttempt"})
in
    #"Removed Columns"
Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.