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
sebastienke
Helper III
Helper III

Calculate values by taking into account filters applied on database

Hi PBI Community, 

 

My problem is not that simply to explain without writting a long message. I'll try to give you just enough details but not too much 😀

 

My problem is the next part of that one :

https://community.powerbi.com/t5/Desktop/How-to-count-quickly-distinct-values-with-a-couple-of-filte...

 

I have a big database (hundreds of thousands of lines) corresponding to articles received in a warehouse. To simplify it, let's say that I juste have those colums :

- Date of receiving

- reference number of the receiving

- Article

- Article version (A, B, C, etc...) and many others columns on which I want to apply slicers for my report

 

NB1 :

1 Receiving reference usually concerns many articles (let's imagine it is a truck delivering many articles) 

NB2:

I can have more than once the same line repeating, it means that I have received more than once the same article with the same receiving reference.

 

My first goal was to create a column in my database calculating, for each line, the distinct number of receiving references received in the same month for the same article. 

 

Here is the formula used, and it works :

Distinct number of receiving references =
CALCULATE (
COUNTROWS ( DISTINCT ( 'Table'[reference number of the receiving] ) ),
ALLEXCEPT ( 'Table', 'Table'[Article], 'Table'[Month] )
)

 

Now, I want in my report a chart illustrating, month by month, the number of distinct articles received with more than one receiving reference. So I have created a measure :

 

What I want = CALCULATE(COUNTROWS(DISTINCT('Table'[Article ]));'Table'[Distinct number of receiving references]>1)

 

It works, but with this way, I cannot apply slicers correclty on my report. For example if I want to chose the "Version of article" as slicer, the result might be wrong.

 

Example : 

DateReceiving referenceArticleVersionDistinct number of reference for the month
Feb#11AppleA2
Feb#12AppleA2
Feb#12AppleB2
Feb#13TomatoA1
March#15AppleA1

 

My chart will display :

  • On February : 1 article received with more than one receiving reference (Apple)
  • On March : 0 article received with more than one receiving reference

But if I add a slicer on my report for chosing the "Version", the result will always be the same, no matter my slicer choice (by chosing Version = B, Feb should be 0, but it is still 1)

 

Any idea how to incorporate my additional filters (=slicers) in my result ?

Any help will really be appreciated, it makes me crazy 😵

 

Thanks a lot

 

 

3 ACCEPTED SOLUTIONS

Hi @sebastienke ,

 

Try the following measure to calculate the values you need:

 

 

Count above 1 =
COUNTROWS (
    FILTER (
        SUMMARIZE (
            'Table';
            'Table'[Article];
            'Table'[Date];
            "@Count@"; COUNTA ( 'Table'[Article] )
        );
        [@Count@] > 1
    )
) + 0

 

 

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

Hi @sebastienke ,

 

Create a column with startofmonth for each datapoint (if your date column is already in the date format the measure below needs to be changed), add a calendar table and relate to that startofmonth then add the following measure and use the months in the calendar table for your visual:

 

Count above 1  + month =
CALCULATE (
    [Count above 1];
    FILTER (
        ALL ( 'calendar'[Date] );
        'calendar'[Date] <= MIN ( 'calendar'[Date] )
            && 'calendar'[Date] >= MINX ( 'calendar'; DATEADD ( 'calendar'[Date]; -1; MONTH ) )
    )
)

 

Check PBIX file attach.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

Hi @sebastienke ,

 

You need to have an extra filter so that you can deselect the values that are not present in the current month so the measure should be redone to this:

 

Received 2+ in 2 months =
IF (
    MIN ( 'calendar'[Date] )
        = CALCULATE ( MIN ( 'calendar'[Date] ); ALL ( 'calendar' ) );
    "Last Month data Missing";
    CALCULATE (
        COUNTROWS (
            FILTER (
                SUMMARIZE (
                    'Table';
                    'Table'[Article];
                    "@Count2+2m@"; COUNTROWS ( DISTINCT ( 'Table'[Receiving reference] ) )
                );
                [@Count2+2m@] >= 2
            )
        );
        FILTER (
            ALL ( 'calendar'[Date] );
            'calendar'[Date] <= MIN ( 'calendar'[Date] )
                && 'calendar'[Date] >= MINX ( 'calendar'; DATEADD ( 'calendar'[Date]; -1; MONTH ) )
        );
        FILTER (
            SUMMARIZE (
                'Table';
                'Table'[Article];
                "@Count2+2m@"; COUNTROWS ( DISTINCT ( 'Table'[Receiving reference] ) )
            );
            [@Count2+2m@] > 0
        )
    ) + 0

 

For the text part you need to add the following measure.

 

Articles selected =
SWITCH (
    TRUE ();
    MIN ( 'calendar'[Date] )
        = CALCULATE ( MIN ( 'calendar'[Date] ); ALL ( 'calendar' ) ); "";
    [Received 2+ in 2 months] = 0; "Nothing";
    CONCATENATEX (
        FILTER (
            SUMMARIZE (
                'Table';
                'Table'[Article];
                "@Count2+2m@"; COUNTROWS ( DISTINCT ( 'Table'[Receiving reference] ) )
            );
            [@Count2+2m@] >= 2
        );
        'Table'[Article];
        ","
    )
)

 

Believe this get's the result you need.

 

Check PBIX file attach.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

17 REPLIES 17

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.