Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Flagging Most Recent Transaction

Goal: Create 2 calculated columns where it will create 1 flag for either the Latest Outstanding Status or Latest Resolved Status. 1 Store = 1 Status Flag (either Outstanding or Resolved)

 

Problem: I am successfull at flagging the most recent status but each store can only have 1 flag. My current output for each calculated column will flag the most recent Outstanding/Resolved status but for stores with both, it will flag both. How can I create the flag where if there is one Resolved status, the entire store is classified as Resolved and flag the most recent one. Same with outstanding. If there are zero resolved status' for the store, the entire store is classified as Outstanding and flag the most recent one. 

 

Below are the following: DAX Calculated Column + Sample Data with Expected Output Flags (notice how each store has only 1 flag). 

Any advice is greatly appreciated. 

 

DAX Resolved Calculated Column Flag >>> (The exact same was created for Outstanding, only thing that changed was "Store[Status] = Resovled" to "Store[Status] <> Resolved") 

 

IF(
    [Date]
    = CALCULATE(
        MAX( [Date] ),
        ALLEXCEPT( Store, Store[store_id] ),
        Store[Status] = "Resolved"
    ), 1
)

 

 

Sample Data with Expected Output Flags

store_idtrans_idstatusdatelatest_outstanding_flaglatest_resolved_flag
1100New1-Jan-21  
1101New12-Jan-21  
1102New20-Feb-211 
2103New1-Jan-21  
2103Resolved2-Jan-21 1
3104New5-Jan-21  
3104Resolved10-Jan-21  
3105New2-Feb-21  
3105In Progress5-Feb-21  
3105Resolved6-Feb-21 1
4106New12-Jan-21  
4106Resolved13-Jan-21 1
4107New1-Mar-21  
4107In Progress2-Mar-21  
5108New10-Mar-21  
5108In Progress11-Mar-21  
5109New16-Mar-21  
5110New22-Mar-211 
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous 

     

    You only have problem with latest_outstanding_flag column or both?

     

    latest_outstanding_flag=
    VAR T1 = FILTER(Store,Store[store_id]=EARLIER(Store[store_id]))
    RETURN
    IF([date]=IF(CONTAINS(T1,Store[status],"Resolved"),BLANK(),MAXX(T1,[date])),1)

     

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    You only have problem with latest_outstanding_flag column or both?

     

    latest_outstanding_flag=
    VAR T1 = FILTER(Store,Store[store_id]=EARLIER(Store[store_id]))
    RETURN
    IF([date]=IF(CONTAINS(T1,Store[status],"Resolved"),BLANK(),MAXX(T1,[date])),1)

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous thank you so much for your support! This was exactly what I needed. Apologies if I was vague or confusing with my description. My only problem was with the latest_outstanding_flag column. The latest_resolved_flag gave me the information needed but I was not able to isolate what your solution provided for the other calculated column. 

      Once again, thank you!

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous Question for you, let's assume that you would like to combine/merge the outputs of latest_outstanding_flag + latest_resolved_flag into 1 calculated column. How would you do that? 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous 

         

        It depends on how you analyse /visualize further with it. Adding it as a DAX calculated column, mark -1 or 1

         

        Column = 
        VAR T1 = FILTER(Store,Store[store_id]=EARLIER(Store[store_id]))
        RETURN
        IF(CONTAINS(T1,Store[status],"Resolved"),
        IF([date]=MAXX(FILTER(T1,[status]="Resolved"),[date]),1),
        IF([date]=MAXX(T1,[date]),-1))

         

        For most of the cases, prefer M for column, here is one way

         

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZLBCsIwDIZfRXpeoUnXqi8gTFDE69hhYvEiG3Sgr++sbMlMd8nl+/jzE1LXClShwJhxnsN7nAb0se00gmqKicJMASXFmaLRh3CbKCZqV5KJXsPQP1/h/lUW8TYpJQW4PGUBYKTiKAB5P6JVt7nE/hHDMPzWZC3e03OlTIpfORJR3tNKZcsvdWqjpH89kVsuWTsqYfJ0mQEgrT1leEGBPgVpf/MB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [store_id = _t, trans_id = _t, status = _t, date = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"store_id", Int64.Type}, {"trans_id", Int64.Type}, {"status", type text}, {"date", type date}}),
            #"Grouped Rows" = Table.Group(#"Changed Type", {"store_id", "status"}, {{"maxDate", each List.Max([date]), type nullable date}}),
            #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"store_id"}, {{"allrows", each _, type table }}),
            #"Added Custom" = Table.AddColumn(#"Grouped Rows1", "res_Date", each List.Max( Table.SelectRows([allrows], each [status] = "Resolved")[maxDate])),
            #"Added Custom1" = Table.AddColumn(#"Added Custom", "max_Date", each if [res_Date] =  null then List.Max([allrows][maxDate]) else null),
            #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"store_id"}, #"Added Custom1", {"store_id"}, "Added Custom1", JoinKind.LeftOuter),
            #"Added Custom2" = Table.AddColumn(#"Merged Queries", "Custom", each if [date] = [Added Custom1][max_Date]{0} then -1 else if [date]=[Added Custom1][res_Date]{0} then 1 else null)
        in
            #"Added Custom2"