Forum Discussion

ToddMate's avatar
ToddMate
Helper II
6 years ago
Solved

Extract latest transaction: Variable attributes.

Hi 

 

I have a table that contains transactions for a service (service_id) as shown below:

 

service_ididdate_createddate_modifieddirectionidnew_plan_idold_plan_idresultusernamewsm
1350529857928/05/201928/05/2019UPGRADE627860046000SUCCESS89926616AFBEB5DCD16
1350529857930/08/201930/08/2019CHURN315255265352653COMPLETED8992665265342
13503529502030/05/201930/05/2019UPGRADE1928346314655SUCCESS5477545E64E1044
13503529502028/09/201928/09/2019DOWNGRADE328865393953939COMPLETED5477545393912

 

What i would like to do is build a visual that shows me just the latest transaction (using the date_created) per service_id.

 

So the outcome would be  just seeing these transactions:

service_ididdate_createddate_modifieddirectionidnew_plan_idold_plan_idresultusernamewsm
1350529857930/08/201930/08/2019CHURN315255265352653COMPLETED8992665265342
13503529502028/09/201928/09/2019DOWNGRADE328865393953939COMPLETED5477545393912

 

Cam anyone assist with this.

Thank you in advance

Todd

  • Hi ToddMate 

     

    Use below measure:

    Measure = var a = CALCULATE(MAX('Table'[date_created]),ALL('Table'),VALUES('Table'[service_id]))
    return
    IF(a=MAX('Table'[date_created]),a,BLANK())

    Then filter the measure is not blank :

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi ToddMate  - 

    First, you can make a new column, which identifies whether the row is the latest for the service_id. There are 2 ways to do this, PowerQuery (M) or DAX. Note: I changed the date format to U.S. so that it would work for me.

    M Script Steps to identify latest:

    1. Use Group By to get the max date for each service_id.
    2. Merge (Left Join) the grouped table into the original table on service_id and date.
    3. Add new column which is False if the merged values are NULL, otherwise true.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZDLDsIgEEX/pWuTzoOhzFIBdeEr1sZF4///hlBjO0ZdnbmZwOEyjg2ygFCzajRIp4UgLYWWAL/CcNld1ymXyVMXKgDcC1DQDzHmvi9TUCXvy4B+vd3kjaSY0DeP1S9daBlmnQ1xP1xPhYxCUijkhQ3j+Xg55FtO1jgtHc0uridJBQhedYzMhqUbKoV6vfOME0Q+yonrOqm1JXuXEZz7J1P7kTak8/301jGFMD2cldXQtluUdYml3eMJ", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [service_id = _t, id = _t, date_created = _t, date_modified = _t, direction = _t, id.1 = _t, new_plan_id = _t, old_plan_id = _t, result = _t, username = _t, wsm = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"service_id", Int64.Type}, {"id", Int64.Type}, {"date_created", type date}, {"date_modified", type date}, {"direction", type text}, {"id.1", Int64.Type}, {"new_plan_id", Int64.Type}, {"old_plan_id", Int64.Type}, {"result", type text}, {"username", Int64.Type}, {"wsm", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"service_id"}, {{"Max Date_Created", each List.Max([date_created]), type text}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"service_id", "date_created"}, #"Grouped Rows", {"service_id", "Max Date_Created"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"service_id"}, {"Grouped Rows.service_id"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Grouped Rows", "Is Latest - M", each [Grouped Rows.service_id] <> null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Grouped Rows.service_id"})
    in
        #"Removed Columns"

     

     DAX Script to identify latest:

    1. Store the service_id of the current row in a variable.
    2. Store the max date for the service_id in a variable.
    3. Return True if the date equals the max date, otherwise false.

     

    Is Latest - DAX = 
    var _current_sid = [service_id]
    var _max_create_date = 
        CALCULATE(
            MAX([date_created]),
            ALL(Table2),
            Table2[service_id] = _current_sid
        )
    return [date_created] = _max_create_date

     

    Then, you can use your new column to filter. The attached PBIX does this with each new column on separate pages.

     

    I hope this helps. If it does, please Mark as a solution.
    I also appreciate Kudos.
    Nathan Peterson
  • v-diye-msft's avatar
    v-diye-msft
    Community Support

    Hi ToddMate 

     

    Use below measure:

    Measure = var a = CALCULATE(MAX('Table'[date_created]),ALL('Table'),VALUES('Table'[service_id]))
    return
    IF(a=MAX('Table'[date_created]),a,BLANK())

    Then filter the measure is not blank :

  • Hi,

    I notice that the result column on the last day is completed for both service_id's.  So just apply a filter on completed in the visual.

    • ToddMate's avatar
      ToddMate
      Helper II

      In this example the status was just coincidental.