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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
CGodfrey
Frequent Visitor

Creating a new column that shows the running total based on multiple conditions

Hey all,

 

I am looking to create a new column that will calculate the running total per unique identifier with multiple conditions.

 

Context: The table I am using is of Gift Card (GC) activations and redemptions. A new row is created each time a new action is logged. All unique GCs have one "Register" but can have multiple actions ( "Increment", "Redeem", "Forced Redeem").

 

Each time a unique card is redeemed ("Redeem" || "Forced Redeem") I want the running total of the number of redemptions to increase.

 

For this sample, I have created four scenarios: 

 

Scenario 1) Serial # 1234 -- Two redemptions on different days

Scenario 2) Serial # 5678 -- Three redemptions on different days

Scenario 3) Serial # 9123 -- Two redemptions on the same day, different times

Scenario 4) Serial # 4567 -- No redemption

 

Desired result in shown in the last column of the table:

 

Serial NumberActionAction DateAction TimeRegister DateDesired Result: Count of
Redeem/Forced redeems
1234Register12-May-2112:00:00 AM12-May-21 
1234Redeem14-May-211:00:00 AM12-May-211
1234Redeem16-May-212:00:00 AM12-May-212
5678Register1-May-2112:00:00 AM1-May-21 
5678Increment2-May-214:00:00 AM1-May-21 
5678Redeem3-May-215:00:00 AM1-May-211
5678Forced Redeem4-May-216:00:00 AM1-May-212
5678Forced Redeem5-May-217:00:00 AM1-May-213
9123Register1-Jan-218:00:00 AM1-Jan-21 
9123Forced Redeem1-Jan-219:00:00 AM1-Jan-211
9123Forced Redeem1-Jan-2110:00:00 AM1-Jan-212
4567Register1-Feb-218:00:00 AM1-Feb-21 

 

I am really trying to learn how DAX is working so I would very much appreciate an explanation of the logic behind the proposed solution. I also wonder about the difference between doing this step in Power Query vs the report. What is the best practice that in terms of performance?

 

Thank you very much for your suggestions!

C

 

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@CGodfrey you can also use RANK function, try following two columns:

 

Action Date Time = Action Date Time = GC[Action Date] + GC[Action Time]

Rank = 
IF ( 
    GC[Is Redeemed] = "Yes",
    RANKX ( 
        FILTER ( 
            ALL ( GC[Serial Number], GC[Action Date Time], GC[Action]  ), 
            GC[Serial Number] = EARLIER ( GC[Serial Number] )  &&  
            SEARCH ( "Redeem", GC[Action], , -1 ) <> -1 
        ),
        GC[Action Date Time] 
        , , ASC 
    )
)

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

6 REPLIES 6
parry2k
Super User
Super User

@CGodfrey you can also use RANK function, try following two columns:

 

Action Date Time = Action Date Time = GC[Action Date] + GC[Action Time]

Rank = 
IF ( 
    GC[Is Redeemed] = "Yes",
    RANKX ( 
        FILTER ( 
            ALL ( GC[Serial Number], GC[Action Date Time], GC[Action]  ), 
            GC[Serial Number] = EARLIER ( GC[Serial Number] )  &&  
            SEARCH ( "Redeem", GC[Action], , -1 ) <> -1 
        ),
        GC[Action Date Time] 
        , , ASC 
    )
)

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hey @parry2k This solution has worked! I am struggling to understand what precisely is happening but good opportunity to wrap my head around RANKX. Thank you for your assistance!

Greg_Deckler
Super User
Super User

@CGodfrey Try:

Column = 
  VAR __SerialNumber = [Serial Number]
  VAR __Date = [Action Date]
RETURN
  COUNTROWS(FILTER('Table',[Serial Number] = __SerialNumber && [Action Date] <= __Date && [Action] = "Redeem"))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler That certainly worked for the "Redeem" action. How could I make it to evaluate for both "Redeem" OR "Foreced Redeem"?

Also, in Scenario 3 above the GC is redeemed twice in the same day so it would have to then look at the Action Time to know which came first that day. 

@CGodfrey Well the first one would be:

Column = 
  VAR __SerialNumber = [Serial Number]
  VAR __Date = [Action Date]
RETURN
  COUNTROWS(FILTER('Table',[Serial Number] = __SerialNumber && [Action Date] <= __Date && ([Action] = "Redeem") || [Action] = "Forced Redeem"))

For the second one, I would recommend creating a new calculated column in Power Query that combines your Date and Time into a single Datetime column and use that instead of [Date] in your formula.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I switched to the date/time combo which resolved some of the errornous results; however, it still evaluates actions that are not equal to "Redeem" or "Forced redeem" and assigns them an additonal value. More specifically, when the action = "Increment" which is very off as I'd expect that would be filtered out with the 

&& ([Action] = "Redeem") || [Action] = "Forced Redeem"))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.