Forum Discussion

ThomasSan's avatar
ThomasSan
Helper IV
3 years ago
Solved

Use double filter when calculating a sum

Hi everyone, 

 

I have the following table:

Order ID

Order DateOrder ValueCustomer

O1

01.01.2021            248,00 €C1
O1619.01.2022               40,00 €C1
O1512.03.2021            112,00 €C2
O915.03.2021            599,00 €C2
O822.04.2022            567,00 €C2
O230.04.2022            457,00 €C1

 

I am creating a company specific view where the user can filter the page with the help of a customer filter. In this company specific view, I would like to create a pie chart in which I can show the share of sales to the selected customer in 2022 to the total. It should look as follows:

 

My DAX formula for C1 works fine*. However, my DAX formula for Rest does not quite work. It looks as follows:

 

Gross Margin CY ALL = 
calculate(
    sum(OrderDetails[Unit Margin]),
    all(),
    filter(Orders,
        year(Orders[Order Date]) = 2022
    )
)

 

 

As you can see, while I would like to keep the date filter, I would like to extend my calculate to all customers. However, I only seem to get the same value returned Can anyone please help me in correcting my DAX formula?


-------------------------

* formula looks as follows:

 

Gross Margin CY= 
calculate(
    sum(OrderDetails[Unit Margin]),
    filter(Orders,
        year(Orders[Order Date]) = 2022
    )
)

 

  • Hi, ThomasSan 

    You can replace the [Rest] measure :

    Rest = 
    VAR _slice = VALUES('Sheet1'[Customer])
    return 
    SUMX( FILTER(ALL(Sheet1) , YEAR('Sheet1'[Order Date]) =2022 && not 'Sheet1'[Customer] in _slice ),'Sheet1'[Order Value]) 

    The result is as follows:

    Best Regards,

    Aniya Zhang

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

     

9 Replies

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi ThomasSan 

    please try

    Gross Margin CY Other =
    CALCULATE (
        SUM ( OrderDetails[Unit Margin] ),
        ALL (),
        FILTER (
            Orders,
            YEAR ( Orders[Order Date] ) = 2022
                && Orders[Customer] <> SELECTEDVALUE ( Orders[Customer] )
        )
    )
    • ThomasSan's avatar
      ThomasSan
      Helper IV

      Hi tamerj1 ,

      thank you for your reply. Unfortunately, I get a (Blank) returned which I do not quite understand. My filter is using Orders[Customer] so I do not quite know why it won't respond to SELECTEDVALUE(Orders[Customer]). Naturally, I checked the data and there are other sales by customers that are not C1 in that year. Do you maybe know where else I could check or what else might help?

       

      Edit:
      I realize that the problem is that in my larger data set, I have more than two customers. I was not aware that my data extract with only two customers may pose an issue here. Do you know how to transform the SELECTEDVALUE(Orders[Customer]) bit to a list? I have already unsuccessfully tried not(Orders[Customers] in {SELECTEDVALUE(Orders[Customer])})

      • tamerj1's avatar
        tamerj1
        Community Champion

        ThomasSan 
        Please try

        Gross Margin CY Other =
        CALCULATE (
            SUM ( OrderDetails[Unit Margin] ),
            ALL (),
            FILTER (
                Orders,
                YEAR ( Orders[Order Date] ) = 2022
                    && NOT ( Orders[Customer] IN VALUE ( Orders[Customer] ) )
            )
        )
  • Hi , ThomasSan 

    You want to show the value of the selected Customer and the value of the other Customer in the pie chart. Right?

    Here are the steps you can refer to :

    (1)This is my test data:

    (2)We can create two measures :

    Custom Value = SUMX( FILTER('Sheet1',YEAR('Sheet1'[Order Date])=2022 ) , 'Sheet1'[Order Value])
    Rest = SUMX( FILTER(ALL(Sheet1) , YEAR('Sheet1'[Order Date]) =2022 && 'Sheet1'[Customer] <> SELECTEDVALUE('Sheet1'[Customer]) ),'Sheet1'[Order Value]) 

    (3)Then we can put the [Customer] in slice and the measure in the pie chart and we will meet your need :

     

    If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

     

    Best Regards,

    Aniya Zhang

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

     

    • ThomasSan's avatar
      ThomasSan
      Helper IV

      Thank you for your response, v-yueyunzh-msft !

      Unfortunately, in my description above, I failed to mention that I have more than two customers. Do you happen to know how to successfully transform the "<> SELECTEDVALUE('Sheet1'[Customer]) " bit into a list?

      • v-yueyunzh-msft's avatar
        v-yueyunzh-msft
        Community Support

        Hi, ThomasSan 

        You can replace the [Rest] measure :

        Rest = 
        VAR _slice = VALUES('Sheet1'[Customer])
        return 
        SUMX( FILTER(ALL(Sheet1) , YEAR('Sheet1'[Order Date]) =2022 && not 'Sheet1'[Customer] in _slice ),'Sheet1'[Order Value]) 

        The result is as follows:

        Best Regards,

        Aniya Zhang

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