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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
thehalfboy
Helper I
Helper I

Power Query: Adding a helper column for comparing multiple rows

Good morning,

 

I've got a set of data where there can be three different scenarios, as shown in the screenshots below:

 

For the requistion number, there is one instance of "Submit" and one instance of "Approve" for 'Action Code':

Example 2.png

For the requisition number, there are multiple instances of "Submit", with one instance of "Approve" below it:

Example 3.png

For the requisition number, there are multiple instances of "Approve" after the "Submit" line:

Example 1.png

I'm specifically trying to create a Yes/No helper column that is Yes only for that final scenario, multiple "Approve" lines after the last "Submit". I'd initially tried doing it by getting rid of any requisitions that only had one "Approve", but that still captured the second scenario. I've tried multiple things, but can't find any way to only find "multiple "Approve"s after the last "Submit""

 

Any help anyone could give would be gratefully received!

1 ACCEPTED SOLUTION

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdG9CsIwEMDxVymZhdxX0ms2X8ChjqWL4OAgFlGf37SIaWokW4Yff+4uw2D6Xg8MwCyeyezM8Xm6Xh7xQa1FtAREDVBgNOPuV++n6X57nWeuiXdBOOMiXtt1HDoLi+YGJaAUdYrnnLASJwv60RCorcVzrttF4y7ls8yai/rfWTaLFniX6i4Q5NyTy2ZB+E4OGoSKOsXXPM5Si2dnQVeLc+LxR7XGXeJ+mWV8Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Requisition Number" = _t, #"Action Code" = _t, #"Action Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Requisition Number", type text}, {"Action Code", type text}, {"Action Date", type datetime}},"en-GB"),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Requisition Number", Order.Ascending}, {"Action Date", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Requisition Number", "Action Code"}, {{"Rows", each _, type table [Requisition Number=nullable text, Action Code=nullable text, Action Date=nullable datetime]}, {"Helper Column", each Table.RowCount(_), Int64.Type}},GroupKind.Local),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Action Date"}, {"Action Date"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Rows",each [Helper Column],each if [Helper Column]=1 then "No" else "Yes",Replacer.ReplaceValue,{"Helper Column"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value",{{"Helper Column", type text}})
in
    #"Changed Type1"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

3 REPLIES 3
thehalfboy
Helper I
Helper I

Requisition NumberDocument StatusAction CodeAction DateHelper Column Notes (Not part of data)
RR8N300334632APPROVEDSubmit27/11/2022 02:31No No because submit line
RR8N300334632APPROVEDApprove28/11/2022 09:43No No because only one approval
RR8N300344687APPROVEDSubmit09/01/2023 14:14No No because submit line
RR8N300344687APPROVEDApprove09/01/2023 14:21No No because prior to a later submit line
RR8N300344687APPROVEDSubmit02/08/2023 10:27No No because submit line
RR8N300344687APPROVEDApprove02/08/2023 10:28No No because a single approval line after the latest submit line
RR8N300334643APPROVEDSubmit27/11/2022 10:23No No because submit line
RR8N300334643APPROVEDApprove28/11/2022 09:14Yes Yes because multiple approvals after a submit line
RR8N300334643APPROVEDApprove29/11/2022 15:20Yes Yes because multiple approvals after a submit line
RR8N300336253APPROVEDSubmit10/08/2023 08:42No No because submit line
RR8N300336253APPROVEDApprove10/08/2023 09:10No No because prior to a later submit line
RR8N300336253APPROVEDSubmit12/08/2023 10:15Yes No because submit line
RR8N300336253APPROVEDApprove13/08/2023 14:18Yes Yes because multiple approvals after a submit line
RR8N300336253APPROVEDApprove15/08/2023 16:10Yes Yes because multiple approvals after a submit line

 

The helper column is what I'm looking to produce, and I've included an extra column on the far right with notes on the logic that has decided whether each line is a Yes or a No in the helper column.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdG9CsIwEMDxVymZhdxX0ms2X8ChjqWL4OAgFlGf37SIaWokW4Yff+4uw2D6Xg8MwCyeyezM8Xm6Xh7xQa1FtAREDVBgNOPuV++n6X57nWeuiXdBOOMiXtt1HDoLi+YGJaAUdYrnnLASJwv60RCorcVzrttF4y7ls8yai/rfWTaLFniX6i4Q5NyTy2ZB+E4OGoSKOsXXPM5Si2dnQVeLc+LxR7XGXeJ+mWV8Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Requisition Number" = _t, #"Action Code" = _t, #"Action Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Requisition Number", type text}, {"Action Code", type text}, {"Action Date", type datetime}},"en-GB"),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Requisition Number", Order.Ascending}, {"Action Date", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Requisition Number", "Action Code"}, {{"Rows", each _, type table [Requisition Number=nullable text, Action Code=nullable text, Action Date=nullable datetime]}, {"Helper Column", each Table.RowCount(_), Int64.Type}},GroupKind.Local),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Action Date"}, {"Action Date"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Rows",each [Helper Column],each if [Helper Column]=1 then "No" else "Yes",Replacer.ReplaceValue,{"Helper Column"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value",{{"Helper Column", type text}})
in
    #"Changed Type1"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.