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
PatrickB
Frequent Visitor

Filter a table based on the MAX value from another query

Hi,

 

I have a problem where I have three tables and I want to filter, in the data load, two of the tables based on the MAX date in the third table. I have tried to used a parameter populated with the MAX date from a query, but it doesn´t seem to work since I can assign the current value the MAX date.

 

I would lika to create a variable that I can populate the MAX date, is that possible? If not, is there any workaround?

 

Rgs,

//Patrick

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @PatrickB,

 

You could achieve this requirement via creating a function which always returns Max date from one table.

 

After loading the first table, right click its date column and choose "Add as a New Query". Then, you will get a date list.

1.PNG

 

Convert this new query to a function in Advanced Editor.

()=>
let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxx\Desktop\Sample Data2.xlsx"), null, true),
    Append1_Sheet = Source{[Item="Append1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Append1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"HeadCount Month", type date}, {"HeadCount", Int64.Type}, {"Attribution Number", Int64.Type}}),
    #"HeadCount Month1" = #"Changed Type"[HeadCount Month],
    #"Max Date" = List.Max(#"HeadCount Month1",1)
in
    #"Max Date"

2.PNG

 

Invoke this function, then, you will get a invoked function (renamed it as GetMaxDate) in the left pane.

3.PNG

 

Now, you can load the second and third table. Modify their query code similar to below, in order to filter date records.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxx\Desktop\Sample Data2.xlsx"), null, true),
    Append1_Sheet = Source{[Item="Append1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Append1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"HeadCount Month", type date}, {"HeadCount", Int64.Type}, {"Attribution Number", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each [HeadCount Month] = GetMaxDate)
in
    #"Filtered Rows"

5.PNG

 

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @PatrickB,

 

You could achieve this requirement via creating a function which always returns Max date from one table.

 

After loading the first table, right click its date column and choose "Add as a New Query". Then, you will get a date list.

1.PNG

 

Convert this new query to a function in Advanced Editor.

()=>
let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxx\Desktop\Sample Data2.xlsx"), null, true),
    Append1_Sheet = Source{[Item="Append1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Append1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"HeadCount Month", type date}, {"HeadCount", Int64.Type}, {"Attribution Number", Int64.Type}}),
    #"HeadCount Month1" = #"Changed Type"[HeadCount Month],
    #"Max Date" = List.Max(#"HeadCount Month1",1)
in
    #"Max Date"

2.PNG

 

Invoke this function, then, you will get a invoked function (renamed it as GetMaxDate) in the left pane.

3.PNG

 

Now, you can load the second and third table. Modify their query code similar to below, in order to filter date records.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxx\Desktop\Sample Data2.xlsx"), null, true),
    Append1_Sheet = Source{[Item="Append1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Append1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"HeadCount Month", type date}, {"HeadCount", Int64.Type}, {"Attribution Number", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each [HeadCount Month] = GetMaxDate)
in
    #"Filtered Rows"

5.PNG

 

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-yulgu-msft,

 

Thanks a lot for your answer! It worked fine!

 

Rgs,

//Patrick

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.