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"
Hi sophie63
I'm not sure if I got your requirements right.
Here is a code for an added column. The code checks, if [STATUS] is closed or not.
You may change the code to your needs.
let
YOUR_LAST_STEP = THE_LAST_LINE_OF_CODE_YOU_WROTE
#"Added Custom" = Table.AddColumn(YOUR_LAST_STEP, "CLOSED_2017", each if [STATUS] ="CLOSED" then "TRUE" else "FALSE")
in
#"Added Custom"
The Query Editor has an even easier Conditional Column option... under the Add Column tab
So the above will generate your M code
However if you want this done with DAX
CLOSED_2017 DAX = IF ( 'Table'[STATUS] = "CLOSED", "TRUE", "FALSE")
You can also use the functions TRUE( ) an FALSE( ) above... don't put those in quotes
CLOSED_2017 DAX = IF ( 'Table'[STATUS] = "CLOSED", TRUE () , FALSE () )
Hope thsi helps! :smileyhappy:
- Anonymous9 years agoNot applicable
One question, three solutions... ;-)
That's Power Query!
- MarcelBeug9 years agoCommunity Champion
Gentlemen Anonymous and Sean
- It's not only Power Query, also DAX.
- By using the "Add Conditional Column" functionality, you get "TRUE"/"FALSE", not the logicals true/false (unless parameters are defined for TRUE and FALSE and selected in the values fields in the screen for adding a conditional column).
- Anonymous9 years agoNot applicable
MarcelBeugand Sean
1. you are right, Marcel
2. the solution has to depend on the customer's requirements (boolean or text). We don't know it at the moment.
She knows it and she will it to us ;-)