Forum Discussion
Power Query Synthax
- 9 years ago
The exact criteria for true or false are still unclear to me, but I gave it a shot.
I created an Excel fle with the data.
In Power BI I created the Power Query code below. It includes explanations (the lines starting with //).
You can copy the code: in Power BI choose "Get Data" - Blank Query - Advanced Editor and replace the default code with the code below. Adjusst the data source to yours, choose "Done" and then - on the Home tab - choose Close & Load.
I also created this video that takes you through the query steps. It is not a live recording of the creation of the query, but a walkthrough after I created the query.
Possibly some detailed adjustment is still required, but this should be close to the final result.
let // Next 3 steps are created when importing the data from Excel Source = Excel.Workbook(File.Contents("C:\Users\Marcel\Documents\Forum bijdragen\Power BI Community\Power Query Syntax - Project status changes.xlsx"), null, true), Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data], #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"PROJECT ID", Int64.Type}, {"STATUS", type text}, {"DATE_VERSION", type text}}), // Add Index for the original sort so the data can be sort back at the end #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Original Sort", 1, 1), // Add column with real date from [DATE VERSION]. "nl-NL" is used so format DD-MM-YYYY is recognized. 31-12-9999 for "LIVE", so it will sort at the end #"Added Custom" = Table.AddColumn(#"Added Index", "Version_Date", each if [DATE_VERSION] = "LIVE" then #date(9999,12,31) else Date.From(Text.Replace([DATE_VERSION],"_","-"),"nl-NL"), type date), // Sort on Project and date #"Sorted Rows" = Table.Sort(#"Added Custom",{{"PROJECT ID", Order.Ascending}, {"Version_Date", Order.Ascending}}), // Add 2 indices so the table can be merged with itself, such that the project and status from the previous row will be on the current row #"Added Index1" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1), #"Added Index2" = Table.AddIndexColumn(#"Added Index1", "Index.1", 1, 1), #"Merged Queries" = Table.NestedJoin(#"Added Index2",{"Index"},#"Added Index2",{"Index.1"},"Previous",JoinKind.LeftOuter), // After the merge, we need the previous project ID and the previous status #"Expanded Previous" = Table.ExpandTableColumn(#"Merged Queries", "Previous", {"PROJECT ID", "STATUS"}, {"Previous.PROJECT ID", "Previous.STATUS"}), // Add a column with true if: the current version = "LIVE" and the Project ID = Previous Project ID and the Status <> Previous Status, else false #"Added Custom1" = Table.AddColumn(#"Expanded Previous", "LIVE Status <> Previous Status?", each [DATE_VERSION] = "LIVE" and [PROJECT ID] = [Previous.PROJECT ID] and [STATUS] <> [Previous.STATUS], type logical), // Sort back to the original sort #"Sorted Rows1" = Table.Sort(#"Added Custom1",{{"Original Sort", Order.Ascending}}), // Remove columns that are no longer required #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows1",{"Original Sort", "Version_Date", "Index", "Index.1", "Previous.PROJECT ID", "Previous.STATUS"}) in #"Removed Columns"
For instance I am trying that, but it's definitely not the right way. First, that does not work. Second, that does does take into account the variation on one particular project between two dates/versions.