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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

TRICKY - Diagonal Sum to Sum a Range of Data

Hi Experts

 

I am trying to sum the range of data as shown in the image below.

Capture.PNG

 

The Sum only applies to the last for Periods as Shown in the image.. 

 

See attached PBIX where i can add single cell - how do you sum a range of cell as shown above

Sample PBIX

https://www.dropbox.com/s/bko95z5uckgj9j3/sample%283%29.pbix?dl=0 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Tricky Sum = 
VAR __inteval = DATESINPERIOD( DATES[Date], MAX( DATES[Date] ), -3, MONTH )
RETURN
    IF(
        MIN( DATES[Date] )
            >= EDATE( CALCULATE( MIN( DATES[Date] ), ALL() ), COUNTROWS( SPAN ) + 1 ),
        SUMX(
            DISTINCT( SPAN[Inteval] ),
            VAR __i = SPAN[Inteval]
            RETURN
                CALCULATE(
                    SUM( 'SAMPLE'[Value] ),
                    'SAMPLE'[Attribute] = __i,
                    DATEADD( __inteval, - SPAN[Inteval], MONTH )
                )
        )
    )

Screenshot 2021-07-03 035408.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

7 REPLIES 7
CNENFRNL
Community Champion
Community Champion

Tricky Sum = 
VAR __inteval = DATESINPERIOD( DATES[Date], MAX( DATES[Date] ), -3, MONTH )
RETURN
    IF(
        MIN( DATES[Date] )
            >= EDATE( CALCULATE( MIN( DATES[Date] ), ALL() ), COUNTROWS( SPAN ) + 1 ),
        SUMX(
            DISTINCT( SPAN[Inteval] ),
            VAR __i = SPAN[Inteval]
            RETURN
                CALCULATE(
                    SUM( 'SAMPLE'[Value] ),
                    'SAMPLE'[Attribute] = __i,
                    DATEADD( __inteval, - SPAN[Inteval], MONTH )
                )
        )
    )

Screenshot 2021-07-03 035408.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Hi CNENFRNL..... How would you do the following - since you original solution was stop on
https://community.powerbi.com/t5/Desktop/TRICKY-Diagonal-Sum-to-Sum-a-Range-of-Data/m-p/1939347#M738...

 

Anonymous
Not applicable

Hi CNENFRNL.... could you kindly have a look at the following please..... stuck and unable to find a solution. 

https://community.powerbi.com/t5/Desktop/TRICKY-Average-last-6-5-4-and-3-months/m-p/1936148#M737666

Anonymous
Not applicable

Many Thanks to all experts

parry2k
Super User
Super User

@Anonymous interesting, solution attached.

 

Sum by Attribute = 
CALCULATE ( 
    SUM ( 'Sample Data'[Value] ), 
    DATESINPERIOD ( 
        'Calendar'[Date], 
        EOMONTH ( 
            MAX ( 'Calendar'[Date] ), 
            MAX ( 'Sample Data'[Attribute] ) * -1
        ),
        -3, 
        MONTH 
    ) 
)


Measure 2 = 
SUMX ( 
    SUMMARIZE ( 
        'Sample Data', 
        'Calendar'[Month], 
        'Sample Data'[Attribute] 
    ), 
    [Sum by Attribute] 
)

parry2k_0-1625253534903.png

 

Check my latest blog post Comparing Selected Client With Other Top N Clients | PeryTUS  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.

parry2k
Super User
Super User

@Anonymous is this the expected output?

 

parry2k_0-1625248051115.png

 



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.

Anonymous
Not applicable

Hi Parry

 

The expected output would be a simple table like this as similir to the table in the PBIX File,

Feb-20 
Mar-20 
Apr-20 
May-20 
Jun-20 
Jul-20 
Aug-20 
Sept-20 
Oct-20147.2
Nov-20152.9
Dec-20148.2
Jan-21162.5

 

The ranges highlighted and the sum formula gives Oct-20.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.