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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
dexter2424
Helper I
Helper I

Distinct Count Total issue based on Type and latest period

Hi All, 

 

I'm struggling with an issue, maybe it's easy to solve, or not, but I'm get lost, and the distinctcount doesn't work properly for me.

I have the following table

Policy NumberReporting PeriodType
A12021-01-01Large
A12021-02-01Not Large
A12021-03-01Large
A12021-05-01Large
B22021-01-01Not Large
B22021-03-01Not Large
B22021-05-01Large


I would like to distinct count the number of "Large" statuses with the following criteria:
I have a slicer, what shows the Max Reporting period date.
Once I select for example 2021-05-01 (YYYY-MM-DD), I would like to see the distinctcount result as 2, because on 2021-05-01 I have 2 Large values.
Once I select 2021-04-01, I would like to see 1, because A1 was Large on 2021-03-01 and B2 was Not Large on 2021-03-01. 
Once I select 2021-02-01, I would like to see 0, because A1 on the selected period is "Not Large", and B2 is also a "Not Large"

In summary, if there is an exact match with the dates, then count as 1 if the Type is "Large", and doesn't count if the Type is "Not Large", 
If there is no record in the selected period (for example A1 on 2021-04-01) then check the second highest date, what is 2021-03-01 and if it's "Large" count as 1, if it's "Not Large" count as 0.

Thank you very much in Advance!

 

Large = 
var _max = CALCULATE(MAXX(Table,Table[Reporting Period]),ALLEXCEPT(Table,Table[Policy Number],Table[Type]))

return

CALCULATE(DISTINCTCOUNT(Table[Policy Number]),FILTER(ALLEXCEPT(Table,Table[Policy Number]),Table[Type] = "Large" && Table[Reporting Period] = _max))

 

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@dexter2424,

 

Try this solution.

 

1. Create date table with a relationship to the data table:

 

DataInsights_4-1623158152797.png

 

2. Create measure:

 

Large = 
VAR vSlicerEndDate =
    LASTDATE ( ALLSELECTED ( Dates[Date] ) )
VAR vResult =
    SUMX (
        VALUES ( FactTable[Policy Number] ),
        VAR vMaxDate =
            CALCULATE (
                MAX ( FactTable[Reporting Period] ),
                FactTable[Reporting Period] <= vSlicerEndDate
            )
        VAR vType =
            CALCULATE ( MAX ( FactTable[Type] ), FactTable[Reporting Period] = vMaxDate )
        RETURN
            IF ( vType = "Large", 1, 0 )
    )
RETURN
    vResult

 

3. Create date slicer using the date table (Dates[Date]).

 

4. Create visual:

 

DataInsights_1-1623157987040.png

 

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

 

DataInsights_2-1623158000229.png

 

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

 

DataInsights_3-1623158045520.png

 

 





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

Proud to be a Super User!




View solution in original post

5 REPLIES 5
DataInsights
Super User
Super User

@dexter2424,

 

Try this solution.

 

1. Create date table with a relationship to the data table:

 

DataInsights_4-1623158152797.png

 

2. Create measure:

 

Large = 
VAR vSlicerEndDate =
    LASTDATE ( ALLSELECTED ( Dates[Date] ) )
VAR vResult =
    SUMX (
        VALUES ( FactTable[Policy Number] ),
        VAR vMaxDate =
            CALCULATE (
                MAX ( FactTable[Reporting Period] ),
                FactTable[Reporting Period] <= vSlicerEndDate
            )
        VAR vType =
            CALCULATE ( MAX ( FactTable[Type] ), FactTable[Reporting Period] = vMaxDate )
        RETURN
            IF ( vType = "Large", 1, 0 )
    )
RETURN
    vResult

 

3. Create date slicer using the date table (Dates[Date]).

 

4. Create visual:

 

DataInsights_1-1623157987040.png

 

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

 

DataInsights_2-1623158000229.png

 

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

 

DataInsights_3-1623158045520.png

 

 





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

Proud to be a Super User!




Hi,

 

I am using this solution and it works great thanks.  I have a quick question.  If I wanted to display this measure in a bar chart instead with the value for each month acros x axis how could that be achieved?  Basically I would like to not have the slider but do the calculation for using the value of the first date of each month instead.  Thanks

Thanks you DataInsights! That's what I'm looking for.

In the meantime I have figured out another solution what is working, I just leave it here, maybe somebody findes it useful:

Creating a max date measure:

Max_Date = CALCULATE(MAXX(Table,Table[Reporting Period]),ALLEXCEPT(Table,Table[Policy Number]))

and the main measure:

Large = COUNTX(FILTER(Table,Table[Type] = "Large" && Table[Reporting Period] = [Max_Date]),Table[Policy Number])



amitchandak
Super User
Super User

@dexter2424 , Try a measure like

 

measure =
Var _max = maxx(allselected(Table), Table[Date])
var _max1 = maxx(filter(Table, Table[date] <_max), Table[Date])
var _cnt = countx(filter(Table, Table[Date] =_max && Table[Type] ="Large"),[Policy Number])+0
var _cnt1 = countx(filter(all(Table), Table[Date] =_max1 && Table[Type] ="Large"),[Policy Number])
return
if(_cnt =0, _cnt1, _cnt)

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thanks Amitchandak, but it still picks up the latest date.

I have 4 different policies, and the highest date is 01-04-2021 there, all of the policies are unique, and for 3 of them the latest date is 01-04-2021 and one of them is 01-03-2021. The result of your query above gives me 3, because just counts the 3 highest ones, where the max date is 01-04-2021 and ignore the 01-03-2021 in another policy

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.