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

How to calculate total 2 weeks from a calculated measure?

Hi all,

 

I have 2 tables:

 

Table 1: Week, National SOM (National SOM is a calculated measure)

Table 2: Week, Sort

 

milkynight_0-1626353567522.pngmilkynight_1-1626353697676.png

 

I want to calculate total of every 2 weeks, for eg:

Jul21 W1 + Jul21 W2 = 4.5+4.6 = 9.1

Jun21 W4 + Jul21 W1 = 4.6+4.5=9.1

 

My current calculation doesn't work for National SOM as a measure (col Total 2W in the first screenshot). I appreciate your help to advise me what I can do in this situation? Thank you.

 

Total 2W = 
CALCULATE (
    'Table 1'[National SOM],
FILTER (
ALL ( 'Table 2'),
'Table 2'[Sort] >= MAX ( 'Table 2'[Sort] ) -1
&& 'Table 2'[Sort] <= MAX ( 'Table 2'[Sort] )
)
)

 

 

1 REPLY 1
daxer-almighty
Solution Sage
Solution Sage

// You must have proper relationships
// in place, e.g., the table with weeks
// must be connected to your fact in a
// one-to-many fashion.

[Total 2W] =
var MaxWeekVisible = MAX( Weeks[Sort] )
var Result = 
    CALCULATE(  
        // You must never precede a measure
        // with the name of the table it's
        // hosted in. On the other hand, columns
        // must always be preceded by the
        // table name.
        [National SOM],
        MaxWeekVisible - 1 <= Weeks[Sort],
        Weeks[Sort] <= MaxWeekVisible,
        ALL( Weeks )
    )
return
    Result
    
// If the above does not work, you have
// to show what your [National SOM] measure
// looks like because it seems to be flawed
// as it does not aggregates across many
// weeks, which stems from the measure's values
// you've shown. Please show the measure
// and the model. And please name your objects
// something that is meaningful. Also,
// it should not be 'Sort' but something like
// WeekID. In formulas do not calculate the
// same things many times over because this
// has the potential to slow down the code.
// If you have a value and need to use it in
// different places---see for example:
// MAX ( 'Table 2'[Sort] )---capture the value
// in a variable and use the variable from then
// on wards (variables are static, they don't
// change once they've been evaluated).
// Please read this guide and follow it:
// https://www.sqlbi.com/articles/rules-for-dax-code-formatting/

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.

Top Solution Authors