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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
APéczely
Frequent Visitor

DAX formula to count ticket status

Dear Community,
I have the following output from a ticketing service shown in the table below:

 

IDCurrent status  CreatedCurrent Status Date  Open  In-progress  Contested  For Review  Closed
10001For review2024.01.01  2024.02.012024.01.01  2024.01.05   2024.02.01 
10002Open2024.01.18  2024.01.182024.01.18    
10003In-progress2024.01.25  2024.01.312024.01.25  2024.01.31  2024.01.28  
10004Contested2024.01.28  2024.02.032024.01.28   2024.02.03  
10005Closed2024.01.16  2024.02.052024.01.16    2024.01.31  2024.02.05

 

The default status of a ticket at creation is "Open". The last status of a ticket is "Closed". In between, the ticket can have any status, but for simplicity we assume that each status is used only once. 

 

Date format is "yyyy.mm.dd".

 

What is the most effective DAX solution to create a time history of the ticket status counts in Power BI as shown in the following bar chart?

 

Status Count Time History.png

 

Any help would be appreciated.

2 ACCEPTED SOLUTIONS
Rupak_bi
Post Prodigy
Post Prodigy

Hi @APéczely 

 

hope below visual displaying right.

Rupak_bi_0-1729507835817.png

The approach is as below

1. unpivot all 5 different stages in power query. 

Rupak_bi_1-1729507978061.png

 

2. create a calender table using min and max date of all the stages

date =
var Min_date = CALCULATE(min('Table'[Value]),all('Table'))
var Max_date = CALCULATE(max('Table'[Value]),all('Table'))

return
CALENDAR(Min_date,Max_date)
 
3. create a calculated table as below
cal_tab =

SUMMARIZECOLUMNS('date'[Date],'Table'[ID],"max date",CALCULATE(max('Table'[Value]),'Table'[Value]<=max('date'[Date])))
5. Add a new column in the above table
new Stat = LOOKUPVALUE('Table'[Attribute],'Table'[Value],cal_tab[max date],'Table'[ID],cal_tab[ID])
6. now create the stack bar chart using the date (X axis), new state (ledgent) and count of new state (Y).
Plz let me know if this works.



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

View solution in original post

@APéczely I was referring to Open Tickets, should have included the reference. Open Tickets - Microsoft Fabric Community This may need to be tweaked based on your particular requirements but the advantage here is that this approach is dynamic.

 

PBIX is attached below signature. This involved a calculated column ( which you could make part of the measure if you wanted )

 

Next = 
    VAR __ID = [ID]
    VAR __Status = [Attribute]
    VAR __Date = [Value]
    VAR __Table = FILTER( 'Table', [ID] = __ID && NOT( [Attribute] IN { __Status, "Created", "Current Status Date" } ) && [Value] >= __Date )
    VAR __MinDate = MINX( __Table, [Value] )
    VAR __Result = IF( __MinDate = BLANK(), MAX( 'Dates'[Date] ), __MinDate )
RETURN
    __Result

 

and the Open Tickets measure:

 

Tickets = 
    VAR __Table =  
        SELECTCOLUMNS(
            FILTER(
                GENERATE(
                    'Table',
                    'Dates'
                ),
                [Date] >= [Value] &&
                [Date] < [Next]
            ),
            "ID",[ID],
            "Date",[Date]
        )
    VAR __Table1 = GROUPBY( __Table,[ID],"Count",COUNTX( CURRENTGROUP(),[Date] )) 
    VAR __Result = COUNTROWS( __Table1 )
RETURN
    __Result

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

15 REPLIES 15
Rupak_bi
Post Prodigy
Post Prodigy

Hi @APéczely ,

I will check your file in some time, meanwhile, I can see, your values have time stamp as well and due to this you are not getting right output. So you need to create a calculated column in the fact table as "new values" and only dates to be extracted from values. Then this new column to be used to create calc_tab. Hope this will solve the issue.



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

@Rupak_bi You were right! 'table'[Value] should be of type date instead of datetime! Thank you for your solution!

Rupak_bi
Post Prodigy
Post Prodigy

Yes . the date column from table cal_tab is used as X -axis. and new stat used as ledgend.  just check it out

Please check your date table formula. the date should start from first january

Rupak_bi_0-1729594024296.png

 

Rupak_bi_1-1729594060788.png

 



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

@Rupak_bi : My date table is fine. If I comment out the 'cal_tab'[max date] column, the 'cal_tab'[date] column is fine. But once I switch it back, 01 January disappears from the 'cal_tab'[date] column and the dates of status changes are incorrect: 

Status Count Time History_4.png

I run Power BI Desktop version: 2.137.751.0 64-bit (October 2024).

 

I share my file in case you have time to have a look on it and spot something I did wrong: Status test.pbix 

Rupak_bi
Post Prodigy
Post Prodigy

Hi, 

Just now I checked my approach and found it is working correctly and there is no date shift. please refer below.

Rupak_bi_0-1729577282496.pngRupak_bi_1-1729577316400.pngRupak_bi_2-1729577334427.png

 



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

@Rupak_bi I can't achieve the same! Which date column do you use for X axis: 'cal_tab'[Date]?
This is my table:
Status Count Time History_3.png
The new Stat column formula:

Status Count Time History_2.png
This is my cal_tab:

Status Count Time History_1.png

ID #10001 starts with Open status on 02 Jan instead of 01 Jan and switched to In-progress on 06 Jan instead of 05 Jan.

Rupak_bi
Post Prodigy
Post Prodigy

Hi @APéczely 

 

hope below visual displaying right.

Rupak_bi_0-1729507835817.png

The approach is as below

1. unpivot all 5 different stages in power query. 

Rupak_bi_1-1729507978061.png

 

2. create a calender table using min and max date of all the stages

date =
var Min_date = CALCULATE(min('Table'[Value]),all('Table'))
var Max_date = CALCULATE(max('Table'[Value]),all('Table'))

return
CALENDAR(Min_date,Max_date)
 
3. create a calculated table as below
cal_tab =

SUMMARIZECOLUMNS('date'[Date],'Table'[ID],"max date",CALCULATE(max('Table'[Value]),'Table'[Value]<=max('date'[Date])))
5. Add a new column in the above table
new Stat = LOOKUPVALUE('Table'[Attribute],'Table'[Value],cal_tab[max date],'Table'[ID],cal_tab[ID])
6. now create the stack bar chart using the date (X axis), new state (ledgent) and count of new state (Y).
Plz let me know if this works.



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

Hi, if my solution works, please accept the reply as solution. 

Truly appreciate your kudos.



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

@Rupak_bi : Almost! The concept of your proposed DAX code works pretty well! But the days are shifted by +1 day at the status change days. For example ticket ID #1001 is open since 2024.01.01, but the start is 2024.01.02 on the chart. Also, the In-progress status is set on 2024.01.05, but the chart shows from 2024.01.06.
I'm trying to find a way to shift everything one day back! 

Hi,

Modify below formula and remove the "=" sign and check.

date",CALCULATE(max('Table'[Value]),'Table'[Value]<=max('date'[Date])))



Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

@Rupak_bi: Unfortunately, removing the "=" sign doesn't change the result at all, there is still +1 day shift.

Greg_Deckler
Super User
Super User

@APéczely In Power Query Editor, select your first 2 columns, right click and Unpivot Other Columns. The rest is trivial at that point.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler, As a matter of fact, it is all but obvious to me after unpivoting the columns you mentioned. How do I count a given status for a given date when I only have the date of status changes? 

@APéczely I was referring to Open Tickets, should have included the reference. Open Tickets - Microsoft Fabric Community This may need to be tweaked based on your particular requirements but the advantage here is that this approach is dynamic.

 

PBIX is attached below signature. This involved a calculated column ( which you could make part of the measure if you wanted )

 

Next = 
    VAR __ID = [ID]
    VAR __Status = [Attribute]
    VAR __Date = [Value]
    VAR __Table = FILTER( 'Table', [ID] = __ID && NOT( [Attribute] IN { __Status, "Created", "Current Status Date" } ) && [Value] >= __Date )
    VAR __MinDate = MINX( __Table, [Value] )
    VAR __Result = IF( __MinDate = BLANK(), MAX( 'Dates'[Date] ), __MinDate )
RETURN
    __Result

 

and the Open Tickets measure:

 

Tickets = 
    VAR __Table =  
        SELECTCOLUMNS(
            FILTER(
                GENERATE(
                    'Table',
                    'Dates'
                ),
                [Date] >= [Value] &&
                [Date] < [Next]
            ),
            "ID",[ID],
            "Date",[Date]
        )
    VAR __Table1 = GROUPBY( __Table,[ID],"Count",COUNTX( CURRENTGROUP(),[Date] )) 
    VAR __Result = COUNTROWS( __Table1 )
RETURN
    __Result

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler: Thanks a lot! This problem intrigued me since my first Power BI report, as much as you were struggling with your Open tickets DAX code. I still need time to understand how the ticket measure works, but this is definitely a solution!

Helpful resources

Announcements
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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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