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

DISTINCOUNT IF certain condition met

Hi All,

I have a table which has Date, Client and Sales columns. The Date column has relationship with "Date" table and I have a date slicer to select certain date range.

I have created 3 measures to calculate the "T-30days sales", "T-60 days sales", and "Diff 30-60" which is basically (T-30) - (T-60).

May I know what is the right DAX formula to calculate the disticnt client who has sales reduced based on Diff 30-60? I used formula like below but it returns an error:

 

30days_Degraded = CALCULATE(DISTINCTCOUNT(Sales[Client]), (Sales[Diff30-60] < 0) , FILTER(ALL('Date'),'Date'[Date]=max('Date'[Date])))
 
Error: A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

 

Thanks.

 

Regards,

Choo

1 ACCEPTED SOLUTION
v-janeyg-msft
Community Support
Community Support

Hello, @ckhoo2007 

 

I create two measures to sum total in pie chart. I also modified the total to make it display correctly. You can check the below sample file.

Like this:

 

Measure1 = 
SUMX (
    ADDCOLUMNS (
        ADDCOLUMNS (
            SUMMARIZE (
                ALLSELECTED(  Sales ),
                Sales[Client],
                "t30",
                    CALCULATE (
                        DIVIDE (
                            SUMX ( 'Sales', 'Sales'[Sales] * 'Sales'[Transactions] ),
                            SUM ( 'Sales'[Transactions] )
                        ),
                        DATESINPERIOD ( Sales[Date], MAX ( Sales[Date] ), -30, DAY )
                    ),
                "t60",
                    CALCULATE (
                        DIVIDE (
                            SUMX ( 'Sales', 'Sales'[Sales] * 'Sales'[Transactions] ),
                            SUM ( 'Sales'[Transactions] )
                        ),
                        DATESINPERIOD ( Sales[Date], MAX ( Sales[Date] ) - 30, -30, DAY )
                    )
            ),
            "t30-t60", [t30] - [t60]
        ),
        "bb", IF ( [t30-t60] > 0, 1 )
    ),
    [bb]
)

 

vjaneygmsft_0-1627021473555.png

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.


Best Regards,

Community Support Team _ Janey

 

View solution in original post

13 REPLIES 13
v-janeyg-msft
Community Support
Community Support

Hello, @ckhoo2007 

 

I create two measures to sum total in pie chart. I also modified the total to make it display correctly. You can check the below sample file.

Like this:

 

Measure1 = 
SUMX (
    ADDCOLUMNS (
        ADDCOLUMNS (
            SUMMARIZE (
                ALLSELECTED(  Sales ),
                Sales[Client],
                "t30",
                    CALCULATE (
                        DIVIDE (
                            SUMX ( 'Sales', 'Sales'[Sales] * 'Sales'[Transactions] ),
                            SUM ( 'Sales'[Transactions] )
                        ),
                        DATESINPERIOD ( Sales[Date], MAX ( Sales[Date] ), -30, DAY )
                    ),
                "t60",
                    CALCULATE (
                        DIVIDE (
                            SUMX ( 'Sales', 'Sales'[Sales] * 'Sales'[Transactions] ),
                            SUM ( 'Sales'[Transactions] )
                        ),
                        DATESINPERIOD ( Sales[Date], MAX ( Sales[Date] ) - 30, -30, DAY )
                    )
            ),
            "t30-t60", [t30] - [t60]
        ),
        "bb", IF ( [t30-t60] > 0, 1 )
    ),
    [bb]
)

 

vjaneygmsft_0-1627021473555.png

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.


Best Regards,

Community Support Team _ Janey

 

Thanks Janey.

v-janeyg-msft
Community Support
Community Support

Hi, @ckhoo2007 

 

There is no context in the pie chart, so there may be problems with the value of the measure.

There is no problem with the table you shared, because these columns are all measures. Can you share a sample fake data sample? So we can help you soon.

vjaneygmsft_0-1626948873720.png

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.


Best Regards,

Community Support Team _ Janey

Thanks Janey and Nidhi.

I have the sample database in excel and sample pbix, stupid question, how do I attached here?

 

Regards,

Choo

Hello, @ckhoo2007 

 

Please upload sample files to onedrive for business, and then send the anonymous link here.

 

Best Regards,

Community Support Team _ Janey

NidhiBhusari
Helper IV
Helper IV

@ckhoo2007 can you share your .pbix file or sample dataset?

NidhiBhusari
Helper IV
Helper IV

@ckhoo2007 can you try implementing this DAX and use date in a slicer.

T = IF([sales total]< 0,DISTINCTCOUNT(Sheet1[Client]))

Here [Sales Total] is Sales[Diff30-60].

Thanks very much Nidhi, yes it works I mean I can calculate the client however it seems like they all returned as Improved, but when I populate the Diff30-60 I can clearly see positive and negative numbers, any reason why?

 

ClientT-30SalesT-60SalesDiff30-60
2D25.8051726.16336-0.3582
A312.2565412.066790.18975
G213.0013412.963190.03815
A825.7510725.88478-0.1337
B211.9176312.65704-0.73941
C110.242669.7926540.45
C214.4651113.350421.11469
D215.8993215.621330.27799
D311.6873811.94896-0.26159
D412.7187412.569130.14961
G411.2975211.081110.21641
G18.9832078.9104480.07276
J115.662115.14740.5147
L933.112834.75687-1.64407
M230.3883930.258230.13017
M814.7373315.79288-1.05555
N312.3325912.296780.03582
N221.5208823.32267-1.80179

 

I used the following formula:

Improved = IF([Diff30-60]> 0,DISTINCTCOUNT(Sheet1[Client]))

Degraded = IF([Diff30-60]< 0,DISTINCTCOUNT(Sheet1[Client]))

 

Thanks again.

 

Regards,

Choo

Sorry some correction, when I try to plot the improved or degraded in a chart it all shows improved.....

ckhoo2007_0-1626774682300.png

 

 

 

 

Thanks Nidhi, let me work out the sample data set and share later.

Hey @ckhoo2007 I tried implementing this scenario that "when I try to plot the improved or degraded in a chart it all shows improved....." using pie chart and as per my analysis I think if your measure generates negative values and are used in pie chart then it won't appear in visual.

but that's why I used Distinctcount which should count only those improved or degraded hence botn are positive numbers.....

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.