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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Bron_B_T
Frequent Visitor

Max custom column

Hi 

 

I have a table which I need to flag the maximum record within a dataset, I then want to filter these records out and make a relationship between this table (which would then only have unique values) with another table which has the ponum listed muliple times.  

 

 

The table of data has the following type structure:

 

ponumrevisionnumConcatenateLatestRecordFlag
28702870FALSE
28712871FALSE
28722872TRUE
109220109220FALSE
109221109221FALSE
109222109222TRUE
114050114050FALSE
114051114051FALSE
114052114052TRUE
252070252070TRUE
271370271370TRUE
311390311390TRUE
323050323050TRUE

 

I can achieve doing a flag within the model stage but I need to take it back to the custom column because I still can't make the tables link.  The formula I am using to flag is working:

 

LatestRecordFlag = 'Purchase Orders'[Concatenate] =
CALCULATE(MAX('Purchase Orders'[Concatenate]),
FILTER(ALL('Purchase Orders'), 'Purchase Orders'[ponum]=EARLIER('Purchase Orders'[ponum])))

 

I need to convert this formula to use in the query editor or to the 'dax' language. The concatenate column is in the query editior already.

 

Please help, thank you for anyones assistance. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Ok I have a way to solve this for you.  We will do all of this in Power Query.

Step 1:  Create a new query, which you will name "fnGetLatestRecord".  Use this code (paste into Advanced Editor)

(table as table, ponum as text) => 
   try Table.Max(Table.SelectRows(table, each [ponum]=ponum),"revisionnum")[revisionnum] otherwise null


Step 2: Make sure you have your original table, which i have labeled as "ExampleTable"

Step 3: Create a new Query, which i have called "LatestTable" but you can call it whatever you like.  Use the following code:

let
    Source = ExampleTable,
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ponum", type text}}),
    #"Add LatestRecord" = Table.AddColumn(#"Changed Type", "LatestRecord", each fnGetLatestRecord(#"Changed Type", [ponum])),
    #"Added Custom" = Table.AddColumn(#"Add LatestRecord", "LatestRecordFlag", each [revisionnum] = [LatestRecord]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([LatestRecordFlag] = true))
in
    #"Filtered Rows"


Now all you need to do is rename "ExampleTable" in the step 3 code, to your real table name.

Optionally you can set your original table (ExampleTable in mine) to not be EnableLoad.  This will hide it in PowerBI

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Ok I have a way to solve this for you.  We will do all of this in Power Query.

Step 1:  Create a new query, which you will name "fnGetLatestRecord".  Use this code (paste into Advanced Editor)

(table as table, ponum as text) => 
   try Table.Max(Table.SelectRows(table, each [ponum]=ponum),"revisionnum")[revisionnum] otherwise null


Step 2: Make sure you have your original table, which i have labeled as "ExampleTable"

Step 3: Create a new Query, which i have called "LatestTable" but you can call it whatever you like.  Use the following code:

let
    Source = ExampleTable,
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ponum", type text}}),
    #"Add LatestRecord" = Table.AddColumn(#"Changed Type", "LatestRecord", each fnGetLatestRecord(#"Changed Type", [ponum])),
    #"Added Custom" = Table.AddColumn(#"Add LatestRecord", "LatestRecordFlag", each [revisionnum] = [LatestRecord]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([LatestRecordFlag] = true))
in
    #"Filtered Rows"


Now all you need to do is rename "ExampleTable" in the step 3 code, to your real table name.

Optionally you can set your original table (ExampleTable in mine) to not be EnableLoad.  This will hide it in PowerBI

Hi there

 

Thank you very much for taking the time to respond and to provide a solution, I've been able to apply this to my dashboard and now have the unique values and the tables linked the way I wanted.

 

Really appreciate this!

Bron_B_T

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.