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
Anno2019
Helper IV
Helper IV

Find Max date and return corresponding row data power query

Dear Guru's

I have been searching for the answer, came close, but each person's needs are slightly different.

I have what seems like a simple task, but alas, I need help.

 

Below is an illustration of what I am dealing with.  I have three tables

1.  Purchasing Data - Contains purchasing data of customer store transactions

2.  Survey Data - Contains survey data of customer survey trends and feedback

3.  Final Result - Contains Customer ID, requires the last transaction with corresponding data elements from both purchasing and survey tables.

I hope the below explains.  Note:  I would prefer to create this in power query mode.

 

MAX Values using different tables.PNG

 

1 ACCEPTED SOLUTION
mattww
Responsive Resident
Responsive Resident

Hi @Anno2019 

 

You could add a grouped index onto your purchase and survey table, which could then be used as a filter to identify the most recent entry per customer. See sample code below:

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tLsnPTS1SMFTSUTIy1Tcw1jcyMAJxgkvyi1KBwrE6SKqMQKrM9Q1M8KoCSRgCzTJFVWWEqQpkowWqKmNMGw2N9A0MUVWZKMXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, #"Purchase Date" = _t, #"Store Name" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer ID", type text}, {"Purchase Date", type date}, {"Store Name", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Purchase Date", Order.Descending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Customer ID"}, {{"Count", each _, type table [Customer ID=nullable text, Purchase Date=nullable date, Store Name=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Purchase Index",1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Customer ID", "Purchase Date", "Store Name", "Purchase Index"}, {"Customer ID.1", "Purchase Date", "Store Name", "Purchase Index"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Customer ID", "Purchase Date", "Store Name", "Purchase Index"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each ([Purchase Index] = 1))
in
#"Filtered Rows"


This is based on the following blog post if you want to work through the steps yourself.

Create Row Number for Each Group in Power BI using Power Query - RADACAD

 

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

3 REPLIES 3
v-shex-msft
Community Support
Community Support

Hi @Anno2019,

Did mattww 's suggestions help with your scenario? if that is the case, you can consider Kudo or accept his suggestions to help others who faced similar requirements.

If that also doesn't help, please share more detailed information to help us clarify your scenario to test.

How to Get Your Question Answered Quickly 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
mattww
Responsive Resident
Responsive Resident

Hi @Anno2019 

 

You could add a grouped index onto your purchase and survey table, which could then be used as a filter to identify the most recent entry per customer. See sample code below:

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tLsnPTS1SMFTSUTIy1Tcw1jcyMAJxgkvyi1KBwrE6SKqMQKrM9Q1M8KoCSRgCzTJFVWWEqQpkowWqKmNMGw2N9A0MUVWZKMXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, #"Purchase Date" = _t, #"Store Name" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer ID", type text}, {"Purchase Date", type date}, {"Store Name", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Purchase Date", Order.Descending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Customer ID"}, {{"Count", each _, type table [Customer ID=nullable text, Purchase Date=nullable date, Store Name=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Purchase Index",1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Customer ID", "Purchase Date", "Store Name", "Purchase Index"}, {"Customer ID.1", "Purchase Date", "Store Name", "Purchase Index"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Customer ID", "Purchase Date", "Store Name", "Purchase Index"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each ([Purchase Index] = 1))
in
#"Filtered Rows"


This is based on the following blog post if you want to work through the steps yourself.

Create Row Number for Each Group in Power BI using Power Query - RADACAD

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

@mattww , so sorry, but I have never tried grouping before, I will try this.

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.