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
stijn977
Helper I
Helper I

DAX Wizards - SUM of balance on oldest date before a given date.

Hello great people of this community. Please hear my tale of what I'm trying to do: 

  1. I have a table looking like, below. Which for each day has an existing user id and their final balance of that day. A user is not necessarily active every day. 
    DateUserBalance
    29/10/2017110
    29/10/2017215
    29/10/201735
    30/10/2017115
    30/10/2017310
    31/10/2017120
    31/10/201725
    31/10/2017320


    2. I want to build a table , that has for each day, the total of all the users ACTIVE on that day, and the total balance of the users NOT-ACTIVE. In this case it would be: 

    DateActive_TotalNon_Active_Total
    29/10/2017300
    30/10/20172515
    31/10/2017450
     
    I can't seem to work it out for it to be relative to the date in the result table. If I use something like MAXDATE, then - in this case - on October 30th, for user 2 it would use balance = 5, where it should be 15 (on october 30th the balance for that user is the balance on the oldest available date <= of october 30th). 

    I've only recently started using PowerBI/DAX but I can't seem to produce the result I want. Any help or tips more than welcome 🙂 

    EDIT: thanks Ashish_Mathur for pointing out the typo in the non-active balance. 
2 ACCEPTED SOLUTIONS

Hi,

 

Here is the PBI desktop version.

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

Hi @stijn977

 

Check this file

 

Use this MEASURE for Non Active Total

 

Non Active Total =
VAR Currentday =
    VALUES ( TableName[Date] )
RETURN
    IF (
        HASONEVALUE ( TableName[Date] ),
        SUMX (
            EXCEPT (
                ALL ( TableName[User] ),
                CALCULATETABLE ( VALUES ( TableName[User] ), TableName[Date] = Currentday )
            ),
            VAR mydate =
                CALCULATE (
                    LASTNONBLANK ( TableName[Date], 1 ),
                    FILTER ( ALL ( TableName[Date] ), TableName[Date] < Currentday )
                )
            RETURN
                CALCULATE (
                    LASTNONBLANK ( TableName[Balance], 1 ),
                    FILTER ( ALL ( TableName[Date] ), TableName[Date] = mydate )
                )
        )
    )

Regards
Zubair

Please try my custom visuals

View solution in original post

23 REPLIES 23

Amazing, I can’t wait to try this! Will definitely let you know the result! 

Zubair_Muhammad
Community Champion
Community Champion

@stijn977

 

Try this Measure

 

Non Active Total =
VAR Priorday =
    PREVIOUSDAY ( VALUES ( Table1[Date] ) )
VAR Currentday =
    VALUES ( Table1[Date] )
RETURN
    IF (
        HASONEVALUE ( Table1[Date] ),
        CALCULATE (
            SUM ( Table1[Balance] ),
            EXCEPT (
                ALL ( Table1[User] ),
                CALCULATETABLE ( VALUES ( Table1[User] ), Table1[Date] = Currentday )
            ),
            Table1[Date] = Priorday
        )
    )

Regards
Zubair

Please try my custom visuals

Hello, thank you for your suggestion. However we can not be sure that we only need to go back one day to find the balance of the users not active today. This could be the previous day, or a week or month ago. So it need to be something along the lines of "get the balance for that users (who are not active today) , where the MAX(date) is < the day you're calculating for,  and add up all the balances you've found for all these users that were not active.  

But thank you for taking the time, I did not know about the EXCEPT functionality, so I might try and experiment along those line! thanks 

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.