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
Anonymous
Not applicable

Count of the last statuses for item

Hello friends,

I have a transactional system where each trx can change status of the client. The status can be changed several times a day for the client. If there are no any trx for several days - client keeps his last status. For example:

 

Tx_idClient_idDate_timeStatus
1CL_1"2020-09-01 09:00:00"New
2CL_2"2020-09-01 10:00:00"New
3CL_1"2020-09-01 22:00:00"Perspective
4CL_2"2020-09-02 10:00:00"Perspective
5CL_3"2020-09-02 12:00:00"New
6CL_3"2020-09-03 15:00:00"Perspective
7CL_1"2020-09-03 22:00:00"VIP
8CL_3"2020-09-03 23:00:00"VIP

 

Expected result: number of clients for each status at the end of day:

DateNewPerspectiveVIP
"2020-09-01"110
"2020-09-02"120
"2020-09-03"012

 

2020-09-01: Client_1 is in Perspective, because it is his last status at the end of day, Client_2 - in New;

2020-09-02: Client_1 is in Perspective, he did not make any trx but he kept his status from the past, Client_2 - in Perspective; Client_3 - in New;

2020-09-03: Client_1 is in VIP, Client_2 - in Perspective, he kept his status from the past; Client_1 - in VIP.

 

Can anyone suggest DAX formula to achieve the result?

1 ACCEPTED SOLUTION
stevedep
Memorable Member
Memorable Member

Hi,

 

The code below does the trick:

 

 

__Counttrx = 
var _selDate = CALCULATE(MAX('Table'[Date_time]), ALL('Table'[Client_id]), ALL('Table'[Status]))
var _selStatus = SELECTEDVALUE('Table'[Status])
var _tblClients = CALCULATETABLE(
                        SUMMARIZE(ALL('Table'),'Table'[Client_id], "MaxDate", CALCULATE(MAX('Table'[Date_time]),  'Table'[Date_time] <= _selDate)))
var _tblClientsStatus = SUMMARIZE(_tblClients,[Client_id],[MaxDate], "Status", COUNTROWS(FILTER(ALL('Table'),'Table'[Client_id]=[Client_id] && 'Table'[Date_time]=[MaxDate] && 'Table'[Status] = _selStatus)))
return
SUMX(_tblClientsStatus,[Status])

 

 

 

As can be seen here:

perspective.jpg

 

Link to the file here.

 

Please mark as solution if so. Thumbs up for the effort is appreciated.

 

In this video I explain how I approach such a challenge. It has some editing issues, hopefully, it's still helpful. 

 

Kind regards,

 

 

Steve. 

 

View solution in original post

3 REPLIES 3
stevedep
Memorable Member
Memorable Member

Hi,

 

The code below does the trick:

 

 

__Counttrx = 
var _selDate = CALCULATE(MAX('Table'[Date_time]), ALL('Table'[Client_id]), ALL('Table'[Status]))
var _selStatus = SELECTEDVALUE('Table'[Status])
var _tblClients = CALCULATETABLE(
                        SUMMARIZE(ALL('Table'),'Table'[Client_id], "MaxDate", CALCULATE(MAX('Table'[Date_time]),  'Table'[Date_time] <= _selDate)))
var _tblClientsStatus = SUMMARIZE(_tblClients,[Client_id],[MaxDate], "Status", COUNTROWS(FILTER(ALL('Table'),'Table'[Client_id]=[Client_id] && 'Table'[Date_time]=[MaxDate] && 'Table'[Status] = _selStatus)))
return
SUMX(_tblClientsStatus,[Status])

 

 

 

As can be seen here:

perspective.jpg

 

Link to the file here.

 

Please mark as solution if so. Thumbs up for the effort is appreciated.

 

In this video I explain how I approach such a challenge. It has some editing issues, hopefully, it's still helpful. 

 

Kind regards,

 

 

Steve. 

 

Anonymous
Not applicable

Hi @stevedep ,

it is not good - it is great!!!

Works with test data. I will try to implement on prod too.

Grateful for your help.

@Anonymous , Welcome!

 

I also created a video to explain how to approach and tackle such a challenge. It has some editing issues, but I hope it's still helpful. It does require quite some DAX knowledge. 

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.

Top Solution Authors