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
NoIdea
Regular Visitor

Basic CALCULATE question OR a strange phenomenon

Hello,

 

I have a question regarding the following simple Measure that does not give the result that I would expect. I would like to know if it is my lack of PBID / DAX understanding (I am new to it) or if there might be something wrong with my data model, PBID settings, etc.

 

 

 

 

Quantity A = 
    CALCULATE ( 
        SUM( fctTable1[Quantity] ), 
        dimTable1[Column1] = "A"
        )

 

 

 

 

I would have expected a Matrix-Visual such as the following (i.e. CALCULATE overwrites the existing filter of dimTable1[Column1] with the new filter for „A“:

(dimTable2[Column1] as rows, dimTable1[Column1] as columns)

 

dimTable1[Column1]

dimTable2[Column1]

A

B

C

ab

1

1

1

cd

4

4

4

ef

7

7

7

Edit: Actually this was also my intention!

 

However, what I got was the following Matrix-Visual (i.e. only the „A“ column was visible):

 

 

dimTable1[Column1]

dimTable2[Column1]

A

ab

1

cd

4

ef

7

 

To verify this result (in case I misunderstood the concept of CALCULATE of replacing a previously existing filter on columns contained in the filter argument) I tried the Measure in a sample file (using the contoso model) from a text book about DAX:

 

 

 

 

Quantity Europe = 
    CALCULATE ( 
        SUM( Sales[Quantity] ), 
        Store[Continent] = "Europe"
        )

 

 

 

 

Surprisingly this time the result was as expected (i.e. all columns were present in the Matrix-Visual, but showing the Quantity for Europe). This was also my original purpose.

 

NoIdea_0-1673549978394.png

 

 

So, the same Measure leads to 2 different visuals. I’m definitely overlooking something, but what?

Has anyone a hint?

 

thanx

1 ACCEPTED SOLUTION
7 REPLIES 7
HotChilli
Super User
Super User

Thanx a lot.

 

I just checked this forum for sort-by-column issues, but not the web.

I definitely don’t understand the code of the queries obviously working in the background in Power Bi or Power Pivot, but I will remember to include the sort-by column in future DAX code.

 

In the current example it worked:

NoIdea_0-1673861066963.png

 

 

NoIdea
Regular Visitor

Meanwhile I have found the reason for this behaviour, but I have no explanation.

The result I get, depends on if the column which is part of the CALCULATE filter argument is being sorted by another column or not.

As an example, I have used a Contoso sample file.

The measures:

SumQuantity = 
    SUM( Sales[Quantity] )

SumQuantity May = 
    CALCULATE(
        SUM( Sales[Quantity] ),
        'Date'[Month] = "May"
    )

 

The visuals without sorting the 'Date'[Month] column:

NoIdea_0-1673715668462.png

 

The visuals with sorting of the 'Date'[Month] column by Month Number:

NoIdea_1-1673715776515.png

An additional Visual combining both measures in a single measure:

NoIdea_3-1673716021788.png

 

Is there anyone out there who can explain this behaviour?

Is this normal?

Should columns not be sorted by other columns?

HotChilli
Super User
Super User

Can you link the pbix please and I'll have a look?

I couldn't figure out how to attach a pbix file, so here is the basic info:

 

Model:

NoIdea_3-1673631864997.png

 

 

Measures:

SumValue = 
    SUM( Results[Value] )

SumValue Start = 
    CALCULATE(
        SUM( Results[Value] ),
        TimePoint[TimePoint_Mo] = "Start"
    )

SumValue Start FILTER = 
    CALCULATE(
        SUM( Results[Value] ),
        FILTER(
            TimePoint,
            TimePoint[TimePoint_Mo] = "Start"
        )
    )

SumValue Start FILTER wALL = 
    CALCULATE(
        SUM( Results[Value] ),
        FILTER(
            ALL( TimePoint[TimePoint_Mo] ),
            TimePoint[TimePoint_Mo] = "Start"
        )
    )

 

Visuals

NoIdea_2-1673631487999.png

 

Meanwhile, my first question is, what should be the result of following measure using a matrix-visual?

SumValue Start = 
    CALCULATE(
        SUM( Results[Value] ),
        TimePoint[TimePoint_Mo] = "Start"
    )

What data should be displayed?

 

amitchandak
Super User
Super User

@NoIdea . Based on what I got, Try like

 

Quantity Europe =
CALCULATE (
SUM( Sales[Quantity] ),
Filter(Stores, Store[Continent] = "Europe")
)

 

Refer: https://youtu.be/cN8AO3_vmlY?t=9300

 

http://dataap.org/blog/2019/04/22/difference-between-calculate-with-and-without-filter-expression/

thanx for the reply.

 

However, I should have explained my original intention a bit more.

 

My intention was that the mentioned Measure diplays the same value for all fields in the Matrix-Visual. The problem was, that it did not do it, as I was expecting.

 

I think I understand the difference to your measure (i.e. using FILTER, because CALCULATE evaluates its filter arguments in the original evaluation context).

However, when I understand correctly my measure could be written with FILTER as well:

 

 

Quantity Europe = 
    CALCULATE ( 
        SUM( Sales[Quantity] ), 
        Store[Continent] = "Europe"
        )

Is the same as

Quantity Europe FILTER = 
    CALCULATE ( 
        SUM( Sales[Quantity] ), 
        FILTER(
            ALL( Store[Continent] ),
            Store[Continent] = "Europe"
        )
    )

 

 

But in my data model (not the contose sample file) it looks like as if the measure 

 

Quantity Europe = 
    CALCULATE ( 
        SUM( Sales[Quantity] ), 
        Store[Continent] = "Europe"
        )

 

behaves like this measure

 

Quantity Europe FILTER woALL = 
    CALCULATE ( 
        SUM( Sales[Quantity] ), 
        FILTER(
            Store,
            Store[Continent] = "Europe"
        )
    )

 

which produces this result, that I do not want:

NoIdea_0-1673602155406.png

 

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.