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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
VoijaRisa
Frequent Visitor

TOPN SUMMARIZE with SWITCH measures

I'm attempting to understand why a measure I've written doesn't work and see if there's something I can do to fix it.

 

Overview of Data & Measures

I'm dealing with shipping data and have a Table visual which has an Origin, Destination, and various numeric measures.

The numeric measures, at their base, are just SUMs of a field in the data, but also do some other tricks:

  • For all, it changes the relationship to the Date table based on a selection from the user (allowing them to filter either by a Ship Date, Invoice Date, or a Processed Date)
  • For currencies, they convert currencies
  • For weights, they convert units

 

Requirement

I am attempting to create Card visuals which displays the SUM of the top 5 rows of this table for the measures described above.

 

My Attempt

As an example, I am using the following measure

 

Top 5 =
SUMX(
    TOPN(
        5,
        SUMMARIZE(
            'Shipments',
            'Shipments'[Origin],
            'Shipments'[Destination],
            "Total Paid", [Total Paid (USD) Sum]),
        [Total Paid], DESC),
    [Total Paid])

 

 

Here's the other measure it's referencing:

 

 

Total Amount Paid (USD) Sum = 
VAR DateSelection = SELECTEDVALUE('Date Slicer Choices'[Date Choice])

VAR TotalPaidInvoiceDate = 
CALCULATE(
    SUM('Shipments'[TOTAL_PAID_USD]), 
    USERELATIONSHIP('Date'[DATE], 'Shipments'[INVOICE_DATE]))

VAR TotalPaidProcessedDate = 
CALCULATE(
    SUM('Shipments'[TOTAL_PAID_USD]), 
    USERELATIONSHIP('Date'[DATE], ' Shipments'[PROCESSED_DATE]))

VAR TotalPaidShipDate = 
CALCULATE(
    SUM('Shipments'[TOTAL_PAID_USD]), 
    USERELATIONSHIP('Date'[DATE], 'Shipments'[SHIP_DATE]))

RETURN
SWITCH(
    TRUE(),
    DateSelection = "Invoice Date", TotalPaidInvoiceDate,
    DateSelection = "Ship Date", TotalPaidShipDate,
    DateSelection = "Processed Date", TotalPaidProcessedDate)

 

 

This measure allows the user to control which of the three date choices. The measure behaves as expected when used in a visual such as the Table visual.

 

In the data model the Date table has three relationships to the Shipment table. The Date[DATE] is joined to Shipment[INVOICE_DATE], Shipment[SHIP_DATE] (the active join), and Shipment[PROCESSED_DATE].

 

My Issue

The Top 5 measure works beautifully when the Date selection is the one that is the active relationship between the Date Table and the Shipment table.

When I select either of the other Date types which are using the inactive relationships, it returns (Blank). I'm quite confused as to why as the subordinate measures (the ones that calculate the Total Paid doing the date SWITCH), both work as expected in the Table visual.

 

Thus, there seems to be some sort of issue passing this second measure into the the first that I'm not understanding.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @VoijaRisa 

 

Based on your detailed description, I have added some steps to the actions you have already taken, and in my simple tests it is working. I hope the following steps will be helpful to you.

 

Here's my data model:

vxianjtanmsft_1-1727160351504.png

 

1. Create the "Total Amount Paid (USD) Sum" measure in the same way as you used it.

2. Create separate measures to calculate Top5 for different date selections.

Top 5 Ship Date = 
SUMX(
    TOPN(
        5,
        ADDCOLUMNS(
            SUMMARIZE(
                'Shipments',
                'Shipments'[Origin],
                'Shipments'[Destination]
            ),
            "Total Paid USD", [Total Amount Paid (USD) Sum]
        ),
        [Total Paid USD], DESC
    ),
    [Total Paid USD]
)

 

Top 5 Invoice Date = 
CALCULATE(
    SUMX(
        TOPN(
            5,
            ADDCOLUMNS(
                SUMMARIZE(
                    'Shipments',
                    'Shipments'[Origin],
                    'Shipments'[Destination]
                ),
                "Total Paid USD", [Total Amount Paid (USD) Sum]
            ),
            [Total Paid USD], DESC
        ),
        [Total Paid USD]
    ),
    USERELATIONSHIP('Date'[DATE], 'Shipments'[INVOICE_DATE])
)

 

Top 5 Processed Date = 
CALCULATE(
    SUMX(
        TOPN(
            5,
            ADDCOLUMNS(
                SUMMARIZE(
                    'Shipments',
                    'Shipments'[Origin],
                    'Shipments'[Destination]
                ),
                "Total Paid USD", [Total Amount Paid (USD) Sum]
            ),
            [Total Paid USD], DESC
        ),
        [Total Paid USD]
    ),
    USERELATIONSHIP('Date'[DATE], 'Shipments'[PROCESSED_DATE])
)

 

3. Combine the Top5 measures created above into one measure.

Top 5 = 
VAR DateSelection = SELECTEDVALUE('Date Slicer Choices'[Date Choice])

RETURN
SWITCH(
    TRUE(),
    DateSelection = "Invoice Date", [Top 5 Invoice Date],
    DateSelection = "Processed Date", [Top 5 Processed Date],
    DateSelection = "Ship Date", [Top 5 Ship Date]
)

 

4. Create a card visual with Top 5 measure.

5. Here is final result:

vxianjtanmsft_2-1727160967065.png

vxianjtanmsft_3-1727160991409.png

vxianjtanmsft_4-1727161019554.png

 

Best Regards,
Jarvis Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @VoijaRisa 

 

Based on your detailed description, I have added some steps to the actions you have already taken, and in my simple tests it is working. I hope the following steps will be helpful to you.

 

Here's my data model:

vxianjtanmsft_1-1727160351504.png

 

1. Create the "Total Amount Paid (USD) Sum" measure in the same way as you used it.

2. Create separate measures to calculate Top5 for different date selections.

Top 5 Ship Date = 
SUMX(
    TOPN(
        5,
        ADDCOLUMNS(
            SUMMARIZE(
                'Shipments',
                'Shipments'[Origin],
                'Shipments'[Destination]
            ),
            "Total Paid USD", [Total Amount Paid (USD) Sum]
        ),
        [Total Paid USD], DESC
    ),
    [Total Paid USD]
)

 

Top 5 Invoice Date = 
CALCULATE(
    SUMX(
        TOPN(
            5,
            ADDCOLUMNS(
                SUMMARIZE(
                    'Shipments',
                    'Shipments'[Origin],
                    'Shipments'[Destination]
                ),
                "Total Paid USD", [Total Amount Paid (USD) Sum]
            ),
            [Total Paid USD], DESC
        ),
        [Total Paid USD]
    ),
    USERELATIONSHIP('Date'[DATE], 'Shipments'[INVOICE_DATE])
)

 

Top 5 Processed Date = 
CALCULATE(
    SUMX(
        TOPN(
            5,
            ADDCOLUMNS(
                SUMMARIZE(
                    'Shipments',
                    'Shipments'[Origin],
                    'Shipments'[Destination]
                ),
                "Total Paid USD", [Total Amount Paid (USD) Sum]
            ),
            [Total Paid USD], DESC
        ),
        [Total Paid USD]
    ),
    USERELATIONSHIP('Date'[DATE], 'Shipments'[PROCESSED_DATE])
)

 

3. Combine the Top5 measures created above into one measure.

Top 5 = 
VAR DateSelection = SELECTEDVALUE('Date Slicer Choices'[Date Choice])

RETURN
SWITCH(
    TRUE(),
    DateSelection = "Invoice Date", [Top 5 Invoice Date],
    DateSelection = "Processed Date", [Top 5 Processed Date],
    DateSelection = "Ship Date", [Top 5 Ship Date]
)

 

4. Create a card visual with Top 5 measure.

5. Here is final result:

vxianjtanmsft_2-1727160967065.png

vxianjtanmsft_3-1727160991409.png

vxianjtanmsft_4-1727161019554.png

 

Best Regards,
Jarvis Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Ashish_Mathur
Super User
Super User

Hi,

I am not sure of how much i can help but i would like to try.  Share the download link of the PBI file.


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

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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