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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

Get Current status based on the latest date and group by id

Hi Team,

 

I need to create a custom column that contains the current status for an id based on the latest date and group by id. please find the below table

 

Stagestatus idForWeekCurrrent Status
ExecuteAmber12/20/2017Amber
Executegreen11/19/2017Amber
Definered22/23/2017Red
Executeamber 202/09/2017Red
Executegreen202/08/2017Red
Defineamber32/17/2017Amber

 

so the end result should be like this

 

Stagestatus idForWeekCurrrent Status
ExecuteAmber12/20/2017Amber
Definered22/23/2017Red
Defineamber32/17/2017Amber

 

please help me with this functionality.

 

Regards,

Sivaaprataap

2 ACCEPTED SOLUTIONS
MarcelBeug
Community Champion
Community Champion

With your data copied to Excel I created the code below in Power Query Excel.

It can be usd in Power BI Query Editor with adjusted Source.

The "SelectLatest" step is a function that is used in the next step to filter the nested tables (in AllData, from the "Grouped Rows"step). The code in "SelectLatest" is based on the code that is generated when you filter a date column on "Is Earliest" (drop-down in column header - Date Filters - Is Earliest)

 

Otherwise the steps are all standard UI steps.

For"#"Changed Type1" I selected all columns and used "Detect Data Type" on the "Transform" tab,  and then adjusted the type for "ForWeek" to date.

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Stage", type text}, {"status ", type text}, {"id", Int64.Type}, {"ForWeek", type datetime}, {"Currrent Status", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"AllData", each _, type table}}),
    SelectLatest = (Group as table) as table => Table.SelectRows(Group, let latest = List.Max(Group[ForWeek]) in each [ForWeek] = latest),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "LatestData", each SelectLatest([AllData])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllData"}),
    #"Expanded LatestData" = Table.ExpandTableColumn(#"Removed Columns", "LatestData", {"Stage", "status ", "ForWeek", "Currrent Status"}, {"Stage", "status ", "ForWeek", "Currrent Status"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded LatestData",{"Stage", "status ", "id", "ForWeek", "Currrent Status"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns",{{"Stage", type text}, {"status ", type text}, {"id", Int64.Type}, {"ForWeek", type date}, {"Currrent Status", type text}})
in
    #"Changed Type1"
Specializing in Power Query Formula Language (M)

View solution in original post

Phil_Seamark
Microsoft Employee
Microsoft Employee

Here is a DAX based solution.  I've assumed your table is called 'Status' and the code below can be used to create a new table from the modelling tab

 

 


New Table = VAR MaxDates = SELECTCOLUMNS( SUMMARIZE('Status','Status'[id] , "Max Date" , MAX('Status'[ForWeek])), "ID2",[id], "Max Date",[Max Date]) VAR r = FILTER( CROSSJOIN('Status',MaxDates),[ID2]=[id] && [Max Date] = [ForWeek]) RETURN SELECTCOLUMNS( r, "Stage",[Stage], "Status",[status], "id",[id], "ForWeek",[ForWeek], "Current Status",[Currrent Status] )

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

View solution in original post

2 REPLIES 2
Phil_Seamark
Microsoft Employee
Microsoft Employee

Here is a DAX based solution.  I've assumed your table is called 'Status' and the code below can be used to create a new table from the modelling tab

 

 


New Table = VAR MaxDates = SELECTCOLUMNS( SUMMARIZE('Status','Status'[id] , "Max Date" , MAX('Status'[ForWeek])), "ID2",[id], "Max Date",[Max Date]) VAR r = FILTER( CROSSJOIN('Status',MaxDates),[ID2]=[id] && [Max Date] = [ForWeek]) RETURN SELECTCOLUMNS( r, "Stage",[Stage], "Status",[status], "id",[id], "ForWeek",[ForWeek], "Current Status",[Currrent Status] )

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

MarcelBeug
Community Champion
Community Champion

With your data copied to Excel I created the code below in Power Query Excel.

It can be usd in Power BI Query Editor with adjusted Source.

The "SelectLatest" step is a function that is used in the next step to filter the nested tables (in AllData, from the "Grouped Rows"step). The code in "SelectLatest" is based on the code that is generated when you filter a date column on "Is Earliest" (drop-down in column header - Date Filters - Is Earliest)

 

Otherwise the steps are all standard UI steps.

For"#"Changed Type1" I selected all columns and used "Detect Data Type" on the "Transform" tab,  and then adjusted the type for "ForWeek" to date.

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Stage", type text}, {"status ", type text}, {"id", Int64.Type}, {"ForWeek", type datetime}, {"Currrent Status", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"AllData", each _, type table}}),
    SelectLatest = (Group as table) as table => Table.SelectRows(Group, let latest = List.Max(Group[ForWeek]) in each [ForWeek] = latest),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "LatestData", each SelectLatest([AllData])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllData"}),
    #"Expanded LatestData" = Table.ExpandTableColumn(#"Removed Columns", "LatestData", {"Stage", "status ", "ForWeek", "Currrent Status"}, {"Stage", "status ", "ForWeek", "Currrent Status"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded LatestData",{"Stage", "status ", "id", "ForWeek", "Currrent Status"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns",{{"Stage", type text}, {"status ", type text}, {"id", Int64.Type}, {"ForWeek", type date}, {"Currrent Status", type text}})
in
    #"Changed Type1"
Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

August Carousel

Fabric Community Update - August 2024

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