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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
ssze
Helper II
Helper II

Count distinct based on multiple conditions

I have a table set (this is a summary selection for testing my DAX) - everything done in 2019

 

2021-03-19_13-54-59.png

I want to do a count distinct of skids that has the latest current_status.

e.g. T5 went from under construction in Feb 19 to Built in Apr-19 then to Temp in Jun-19.

whereas Skid 33 went from under construction in Feb 19 to built in Apr-19.

(there will be cases where the latest status is only under construction as well).

I have been googling to check filter etc but am at a loss!

Just wonder if anyone has experience in this sort of double filtering (latest date and latest current status)?

 

Thank you in advance!

1 ACCEPTED SOLUTION

@ssze ,

As usual, there are multiple ways in DAX to achieve the result and it depends on the model and the requirements. You can try the next option (it might be not the best one, just try to play with it):

 

VAR mostRecentSkids =
    SUMMARIZECOLUMNS ( SkidTable3[Forest], SkidTable3[Skid],
        "mostRecentDate", CALCULATE ( MAX ( SkidTable3[Month] ) )
    )
VAR filteredSkidTable =
    CALCULATETABLE (
        SkidTable3,
        TREATAS ( MostRecentSkids, SkidTable3[Forest], SkidTable3[Skid], SkidTable3[Month] )
    )
RETURN
    SUMMARIZECOLUMNS ( SkidTable3[Forest], SkidTable3[Current_status],
        "Skids",
            VAR SkidsAmt = COUNTROWS (
                    FILTER (
                        filteredSkidTable,
                        [Forest] = SELECTEDVALUE ( SkidTable3[Forest] )
                            && [Current_status] = SELECTEDVALUE ( SkidTable3[Current_status] )
                    )
                )
            RETURN COALESCE ( SkidsAmt, 0 )
    )

 

By the way, I've also changed the previous one as I've got that it is not recommended to use summarize function to add columns.

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

View solution in original post

6 REPLIES 6
ERD
Super User
Super User

Hello @ssze ,

I'm not sure about a measure, but in case of a calculated table here is the code that will give the expected output:

EDITED (It appears that SUMMARIZE is not recommended to be used to add columns)

 

 

var latestStatusPerSkid  = 
SUMMARIZECOLUMNS (
SkidTable[Skid], 
	"CS",
	MAXX( FILTER(SkidTable, SkidTable[Month] = MAX(SkidTable[Month]))
	,SkidTable[Current_status])
)
RETURN
ADDCOLUMNS(
VALUES(SkidTable[Current_status]), 
"Skids", 
COUNTROWS(FILTER(latestStatusPerSkid, SkidTable[Current_status] = [CS]))
)

 

 

 

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

Thank you heaps @ERD ! it worked! 

One more question though - my DAX writing skills isn't quite there yet and i tried a few different ways, but my forest column in the table has multiple forests usually. How can i addcolumns with a forest too? 

 

i.e. with your help i have this, and would like an extra column there for forests (so Forest A, Built, Under Construction, Temp; Forest B, Built, Under Construction, Temp etcetc)

Thank you heaps!

ssze_0-1617069606898.png

 

@ssze ,

As usual, there are multiple ways in DAX to achieve the result and it depends on the model and the requirements. You can try the next option (it might be not the best one, just try to play with it):

 

VAR mostRecentSkids =
    SUMMARIZECOLUMNS ( SkidTable3[Forest], SkidTable3[Skid],
        "mostRecentDate", CALCULATE ( MAX ( SkidTable3[Month] ) )
    )
VAR filteredSkidTable =
    CALCULATETABLE (
        SkidTable3,
        TREATAS ( MostRecentSkids, SkidTable3[Forest], SkidTable3[Skid], SkidTable3[Month] )
    )
RETURN
    SUMMARIZECOLUMNS ( SkidTable3[Forest], SkidTable3[Current_status],
        "Skids",
            VAR SkidsAmt = COUNTROWS (
                    FILTER (
                        filteredSkidTable,
                        [Forest] = SELECTEDVALUE ( SkidTable3[Forest] )
                            && [Current_status] = SELECTEDVALUE ( SkidTable3[Current_status] )
                    )
                )
            RETURN COALESCE ( SkidsAmt, 0 )
    )

 

By the way, I've also changed the previous one as I've got that it is not recommended to use summarize function to add columns.

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

Thank you heaps - it worked wonders 🙂

ssze
Helper II
Helper II

Thank you @amitchandak!

here is the input table (i think don't worry about OBJECTID because it's from a database)

OBJECT IDForestSkidCurrent_StatusPrevious StatusMonth
59A35BuiltPlannedFeb-19
51A37BuiltPlannedMar-19
113A33BuiltUnder ConstructionApr-19
114A34BuiltUnder ConstructionApr-19
115AT5BuiltUnder ConstructionApr-19
179AT7BuiltPlannedAug-19
180A32ABuiltPlannedAug-19
181A1BuiltPlannedAug-19
149AT5TempBuiltJun-19
60AT5Under Construction-Feb-19
61A34Under ConstructionPlannedFeb-19
62A33Under ConstructionPlannedFeb-19

 

and the output i am looking for would be like this:

(counting the number of skids according to the latest status)

Current StatusNumber of SkidsMy Note
Built7Skid 35, 37,33 ,34, T7,32A,1 (because T5 latest status is Temp)
Temp1T5
Under Construction0Because all 3 are now built or is temp

 

I hope this makes sense 🙂 Thank you again for your help.

 

Cheers,Silvia

amitchandak
Super User
Super User

@ssze , the expected output is not very clear.

 

Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

Top Solution Authors