Forum Discussion

Renee_Mas's avatar
Renee_Mas
New Member
3 years ago
Solved

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 colu...
  • ImkeF's avatar
    3 years ago

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

    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