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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Renee_Mas
New Member

Checking if value in one column matches value in another column

Hello PowerBI Experts,

I am new to PowerBI and trying to solve one issue.  I am sure the solution is obvious to all experts here:)

My data looks like this:

I need to create the Highest Status column using the following logic.

Renee_Mas_1-1666488536492.png

 

For each order number, the Highest Stage should be captured from Status when the stage in equal to highest stage.

Each order had candidates who went through numerous stages but all end at the last stage. Some orders have more stages than others so Highest Stage could be any number. 

I am only interested in the final (higest stage) for each order.

Any hints are appreciated.

Thank you,

Renee

 

 

 

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @Renee_Mas ,
you can group your data on order number to retrieve the maximum value like so:

ImkeF_0-1666506401967.png

then expand the "Partition" Column to get back the fields that haven't been grouping columns.
That will return the higest stage for all fields.
Now, if you want to identify only those rows who were the higest stage, you can add additional column with formulas like this:
if [Stage] = [Hightest Stage] then [Hightest Stage] else null

 

You can also paste the following code into the advanced editor and follow the steps:

 

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WMjYxUtJR8k9LSy1KTQGyTJRidTBFjcCihkbGZriFXVKTczLzYIbEAgA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [#"Order Number" = _t, Status = _t, Stage = _t]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {{"Order Number", Int64.Type}, {"Status", type text}, {"Stage", Int64.Type}}
    ),
    #"Grouped Rows" = Table.Group(
        #"Changed Type",
        {"Order Number"},
        {
            {"Hightest Stage", each List.Max([Stage]), type nullable number},
            {
                "Partition",
                each _,
                type table [Order Number = nullable number, Status = nullable text, Stage = nullable number]
            }
        }
    ),
    #"Expanded Partition" = Table.ExpandTableColumn(
        #"Grouped Rows",
        "Partition",
        {"Status", "Stage"},
        {"Status", "Stage"}
    ),
    #"Added Custom" = Table.AddColumn(
        #"Expanded Partition",
        "Is Higest Stage",
        each if [Stage] = [Hightest Stage] then [Hightest Stage] else null
    ),
    #"Added Custom1" = Table.AddColumn(
        #"Added Custom",
        "Highest Status",
        each if [Is Higest Stage] = null then null else [Status]
    )
in
    #"Added Custom1"


Next time, please provide sample data so the answerers don't have to create them themselves:
How to provide sample data in the Power BI Forum - Microsoft Power BI Community


 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

4 REPLIES 4
ImkeF
Super User
Super User

Hi @Renee_Mas ,
you can group your data on order number to retrieve the maximum value like so:

ImkeF_0-1666506401967.png

then expand the "Partition" Column to get back the fields that haven't been grouping columns.
That will return the higest stage for all fields.
Now, if you want to identify only those rows who were the higest stage, you can add additional column with formulas like this:
if [Stage] = [Hightest Stage] then [Hightest Stage] else null

 

You can also paste the following code into the advanced editor and follow the steps:

 

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WMjYxUtJR8k9LSy1KTQGyTJRidTBFjcCihkbGZriFXVKTczLzYIbEAgA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [#"Order Number" = _t, Status = _t, Stage = _t]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {{"Order Number", Int64.Type}, {"Status", type text}, {"Stage", Int64.Type}}
    ),
    #"Grouped Rows" = Table.Group(
        #"Changed Type",
        {"Order Number"},
        {
            {"Hightest Stage", each List.Max([Stage]), type nullable number},
            {
                "Partition",
                each _,
                type table [Order Number = nullable number, Status = nullable text, Stage = nullable number]
            }
        }
    ),
    #"Expanded Partition" = Table.ExpandTableColumn(
        #"Grouped Rows",
        "Partition",
        {"Status", "Stage"},
        {"Status", "Stage"}
    ),
    #"Added Custom" = Table.AddColumn(
        #"Expanded Partition",
        "Is Higest Stage",
        each if [Stage] = [Hightest Stage] then [Hightest Stage] else null
    ),
    #"Added Custom1" = Table.AddColumn(
        #"Added Custom",
        "Highest Status",
        each if [Is Higest Stage] = null then null else [Status]
    )
in
    #"Added Custom1"


Next time, please provide sample data so the answerers don't have to create them themselves:
How to provide sample data in the Power BI Forum - Microsoft Power BI Community


 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hello Imke,

 

My problem became a bit more complex and now for each order number, I need to identify the Highest Application Status (last column in the data sample) per order per candidate. 

I need to identify the highest stage highest stage each candiate has attained (which I did) and 

what that highest stage was (per order number, per candidate).

As before, each order had candidates who went through numerous stages but each ends up at the last stage. Some orders have more stages than others so Highest Stage could be any number. 

I am only interested in the final (higest stage) for each order.

 

Thank you,

ps.  I liked your previous solution and wonder if that one extra step might be obvious to you. 

I will also post it again.

Thank you for all the hits and pointers so far!

 

Renee

Order IDCandidate name Application StatusStage Highest Stage for candidate Higest Application Status 
1AmbaNew1 6 
1AmbaOffer Accepted6 6Offer Accepted 
1AmbaOffer Cancelled3 6 
1AmbaOffer Pending Approval4 6 
1AmbaOffer presented2 6 
1AmbaOffer Sent5 6 
1HenrietteNew1 1New
1JaspreetDeclined3 3Declined 
1JaspreetNew1 3 
1JaspreetOffered2 3 
1KristaNew1 4 
1KristaOffer Pending Approval4 4Offer Pending Approval 
1KristaPre-Screen2 4 
1KristaPresented to HM 3 4 
1SorusNew1 1New
6ClaudiaDeclined2 2Declined 
6ClaudiaNew1 2 
6MadisonNew1 2 
6MadisonOffered2 2Offered
6NadiaDeclined3 3Declined 
6NadiaNew1 3 
6NadiaRejected by TAP2 3 
9RupinderNew1 1New
9SukhmanNew1 2 
9SukhmanPre-Screen2 2Pre-screen
9VictoriaDeclined2 3 
9VictoriaDeclined3 3Declined 
9VictoriaNew1 3 
12ADELDeclined3 3Declined 
12ADELIn-Progress2 3 
12ADELNew1 3 
12ALIREZADeclined3 3Declined 
12ALIREZAIn-Progress2 3 
12ALIREZANew1 3 
12ASSEMDeclined3 3Declined 
12ASSEMIn-Progress2 3 
12ASSEMNew1 3 
12DIANENew1 3 
12DIANEOffer presented to candidate2 3 
12DIANEOffered3 3Offered

Hello Imke,

 

Thank you. The solution works. Thank you so much.

My problem became a bit more complex with another variable and I will try to outline by attaching a new file as per your instructions. 

Thanks again for your prompt response!

Renee 

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors