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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
Ankita26
Regular Visitor

I want to get Min and Max date and where the category changes in between the dates

Tank Capacity from 70% to 85% falls under Exception Yellow category, Tank capacity >85% falls under category Exception red

 

We want to calculate start date and end date based on their number of occurrence and whenever the exception changes and calculate Max tank capacity in between those dates

 

 

Raw Data

 

MaterialLocationException CategoryDateTank_Capacity
C150Chane BotlekEXCEPTION RED3/24/2025 0:0099.17%
C150Chane BotlekEXCEPTION RED3/25/2025 0:0098.89%
C150Chane BotlekEXCEPTION RED3/26/2025 0:0098.61%
C150Chane BotlekEXCEPTION RED3/27/2025 0:0098.34%
C150Chane BotlekEXCEPTION RED3/28/2025 0:0098.06%
C150Chane BotlekEXCEPTION RED3/29/2025 0:0097.78%
C150Chane BotlekEXCEPTION RED3/30/2025 0:0097.50%
C150Chane BotlekEXCEPTION RED3/31/2025 0:0097.22%
C150Chane BotlekEXCEPTION RED4/1/2025 0:0096.83%
C150Chane BotlekEXCEPTION RED4/2/2025 0:0093.48%
C150Chane BotlekEXCEPTION RED4/3/2025 0:0093.10%
C150Chane BotlekEXCEPTION RED4/4/2025 0:0092.71%
C150Chane BotlekEXCEPTION RED4/5/2025 0:0092.33%
C150Chane BotlekEXCEPTION RED4/6/2025 0:0091.94%
C150Chane BotlekEXCEPTION YELLOW4/7/2025 0:0082.29%
C150Chane BotlekEXCEPTION YELLOW4/8/2025 0:0081.91%
C150Chane BotlekEXCEPTION YELLOW4/9/2025 0:0077.81%
C150Chane BotlekEXCEPTION YELLOW4/10/2025 0:0077.43%
C150Chane BotlekEXCEPTION YELLOW4/11/2025 0:0077.04%
C150Chane BotlekEXCEPTION YELLOW4/12/2025 0:0076.66%
C150Chane BotlekEXCEPTION YELLOW4/13/2025 0:0074.42%
C150Chane BotlekEXCEPTION YELLOW4/14/2025 0:0074.04%
C150Chane BotlekEXCEPTION YELLOW4/15/2025 0:0073.65%
C150Chane BotlekEXCEPTION YELLOW4/16/2025 0:0073.27%
C150Chane BotlekEXCEPTION YELLOW4/17/2025 0:0072.88%
C150Chane BotlekEXCEPTION YELLOW4/18/2025 0:0072.50%
C150Chane BotlekEXCEPTION YELLOW4/19/2025 0:0072.11%
C150Chane BotlekEXCEPTION YELLOW4/20/2025 0:0071.72%
C150Chane BotlekEXCEPTION YELLOW4/21/2025 0:0071.34%
C150Chane BotlekEXCEPTION YELLOW4/22/2025 0:0070.95%
C150Chane BotlekEXCEPTION YELLOW4/23/2025 0:0070.57%
C150Chane BotlekEXCEPTION YELLOW4/24/2025 0:0070.18%
C150Chane BotlekEXCEPTION RED5/5/2025 0:00100.85%
C150Chane BotlekEXCEPTION RED5/6/2025 0:00100.47%
C150Chane BotlekEXCEPTION RED5/7/2025 0:00100.10%
C150Chane BotlekEXCEPTION RED5/8/2025 0:0099.73%
C150Chane BotlekEXCEPTION RED5/9/2025 0:0099.35%
C150Chane BotlekEXCEPTION RED5/10/2025 0:0098.98%
C150Chane BotlekEXCEPTION RED5/11/2025 0:0098.61%
C150Chane BotlekEXCEPTION RED5/12/2025 0:0098.24%
C150Chane BotlekEXCEPTION RED5/13/2025 0:0097.86%
C150Chane BotlekEXCEPTION RED5/14/2025 0:0097.49%
C150Chane BotlekEXCEPTION RED5/15/2025 0:0097.12%
C150Chane BotlekEXCEPTION RED5/16/2025 0:0096.75%
C150Chane BotlekEXCEPTION RED5/17/2025 0:0096.37%
C150Chane BotlekEXCEPTION RED5/18/2025 0:0096.00%
C150Chane BotlekEXCEPTION YELLOW5/19/2025 0:0080.80%
C150Chane BotlekEXCEPTION YELLOW5/20/2025 0:0080.43%
C150Chane BotlekEXCEPTION YELLOW5/21/2025 0:0080.06%
C150Chane BotlekEXCEPTION YELLOW5/22/2025 0:0079.68%
C150Chane BotlekEXCEPTION YELLOW5/23/2025 0:0079.31%
C150Chane BotlekEXCEPTION YELLOW5/24/2025 0:0078.94%
C150Chane BotlekEXCEPTION YELLOW5/25/2025 0:0078.57%
C150Chane BotlekEXCEPTION YELLOW5/26/2025 0:0078.19%
C150Chane BotlekEXCEPTION YELLOW5/27/2025 0:0077.82%
C150Chane BotlekEXCEPTION YELLOW5/28/2025 0:0077.45%
C150Chane BotlekEXCEPTION YELLOW5/29/2025 0:0077.08%
C150Chane BotlekEXCEPTION YELLOW5/30/2025 0:0076.70%
C150Chane BotlekEXCEPTION YELLOW5/31/2025 0:0076.33%
C150Chane BotlekEXCEPTION YELLOW6/1/2025 0:0075.94%
C150Chane BotlekEXCEPTION YELLOW6/2/2025 0:0075.56%
C150Chane BotlekEXCEPTION YELLOW6/3/2025 0:0075.17%
C150Chane BotlekEXCEPTION YELLOW6/4/2025 0:0074.79%
C150Chane BotlekEXCEPTION YELLOW6/5/2025 0:0074.40%
C150Chane BotlekEXCEPTION YELLOW6/6/2025 0:0074.02%
C150Chane BotlekEXCEPTION YELLOW6/7/2025 0:0073.63%
C150Chane BotlekEXCEPTION RED6/8/2025 0:00136.62%
C150Chane BotlekEXCEPTION RED6/9/2025 0:00136.24%
C150Chane BotlekEXCEPTION RED6/10/2025 0:00135.85%
C150Chane BotlekEXCEPTION RED6/11/2025 0:00135.47%
C150Chane BotlekEXCEPTION RED6/12/2025 0:00135.08%
C150Chane BotlekEXCEPTION RED6/13/2025 0:00134.70%
C150Chane BotlekEXCEPTION RED6/14/2025 0:00134.31%
C150Chane BotlekEXCEPTION RED6/15/2025 0:00133.92%
C150Chane BotlekEXCEPTION RED6/16/2025 0:00133.54%
C150Chane BotlekEXCEPTION RED6/17/2025 0:00133.15%
C150Chane BotlekEXCEPTION RED6/18/2025 0:00132.77%

 

Expected result is

 

MaterialLocationException CategoryStart DateEnd DateMax Tank_Capacity
C150Chane BotlekEXCEPTION RED3/24/2025 0:004/6/2025 0:0099.17%
C150Chane BotlekEXCEPTION YELLOW4/7/2025 0:004/24/2025 0:0082.29%
C150Chane BotlekEXCEPTION RED5/5/2025 0:005/18/2025 0:00100.85%
C150Chane BotlekEXCEPTION YELLOW5/19/2025 0:006/7/2025 0:0080.80%
C150Chane BotlekEXCEPTION RED6/8/2025 0:006/18/2025 0:00136.62%
1 ACCEPTED SOLUTION
xifeng_L
Super User
Super User

Hi @Ankita26 

 

You can add two calculated columns as below :

 

xifeng_L_0-1742889934828.png

Start Date = 
VAR Tbl = CALCULATETABLE('Table',ALLEXCEPT('Table','Table'[Material],'Table'[Location]))
VAR PreStartDate = 
    MAXX(
        FILTER(
            Tbl,
            AND(
                'Table'[Date]<EARLIER('Table'[Date]),
                'Table'[Exception Category]<>EARLIER('Table'[Exception Category])
            )
        ),
        'Table'[Date]
    )
RETURN
COALESCE(
    MINX(FILTER(Tbl,'Table'[Date]>PreStartDate ),'Table'[Date]),
    MINX(Tbl,'Table'[Date])
)
End Date = 
VAR Tbl = CALCULATETABLE('Table',ALLEXCEPT('Table','Table'[Material],'Table'[Location]))
VAR NextEndDate = 
    MINX(
        FILTER(
            Tbl,
            AND(
                'Table'[Date]>EARLIER('Table'[Date]),
                'Table'[Exception Category]<>EARLIER('Table'[Exception Category])
            )
        ),
        'Table'[Date]
    )
RETURN
COALESCE(
    MAXX(FILTER(Tbl,'Table'[Date]<NextEndDate),'Table'[Date]),
    MAXX(Tbl,'Table'[Date])
)

 

Then, create a table to get the desired results:

 

xifeng_L_1-1742890032933.png

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

View solution in original post

11 REPLIES 11
techies
Solution Supplier
Solution Supplier

Hi @Ankita26 

 

Before creating measures for Start Date, End Date, and Tank Capacity, I guess you need to first create calculated columns like this to structure the data properly.

 

Category Change =
VAR PrevCategory =
    CALCULATE(
        MAX('Sheet1'[Exception Category]),
        FILTER(
            'Sheet1',
            'Sheet1'[Material] = EARLIER('Sheet1'[Material]) &&
            'Sheet1'[Location] = EARLIER('Sheet1'[Location]) &&
            'Sheet1'[Date] < EARLIER('Sheet1'[Date])
        )
    )
RETURN
IF(
    PrevCategory <> 'Sheet1'[Exception Category], 1, 0
)
 
 
Group ID =
VAR RunningTotal =
    SUMX(
        FILTER(
            'Sheet1',
            'Sheet1'[Material] = EARLIER('Sheet1'[Material]) &&
            'Sheet1'[Location] = EARLIER('Sheet1'[Location]) &&
            'Sheet1'[Date] <= EARLIER('Sheet1'[Date])
        ),
        'Sheet1'[Category Change]
    )
RETURN
RunningTotal

Hi @techies Thanks for the solution. But in my dataset I am not able to create a new column in my file. And the earlier function doesnot work when we use a measure.

Ankita26_0-1742967248762.png

 

xifeng_L
Super User
Super User

Hi @Ankita26 

 

You can add two calculated columns as below :

 

xifeng_L_0-1742889934828.png

Start Date = 
VAR Tbl = CALCULATETABLE('Table',ALLEXCEPT('Table','Table'[Material],'Table'[Location]))
VAR PreStartDate = 
    MAXX(
        FILTER(
            Tbl,
            AND(
                'Table'[Date]<EARLIER('Table'[Date]),
                'Table'[Exception Category]<>EARLIER('Table'[Exception Category])
            )
        ),
        'Table'[Date]
    )
RETURN
COALESCE(
    MINX(FILTER(Tbl,'Table'[Date]>PreStartDate ),'Table'[Date]),
    MINX(Tbl,'Table'[Date])
)
End Date = 
VAR Tbl = CALCULATETABLE('Table',ALLEXCEPT('Table','Table'[Material],'Table'[Location]))
VAR NextEndDate = 
    MINX(
        FILTER(
            Tbl,
            AND(
                'Table'[Date]>EARLIER('Table'[Date]),
                'Table'[Exception Category]<>EARLIER('Table'[Exception Category])
            )
        ),
        'Table'[Date]
    )
RETURN
COALESCE(
    MAXX(FILTER(Tbl,'Table'[Date]<NextEndDate),'Table'[Date]),
    MAXX(Tbl,'Table'[Date])
)

 

Then, create a table to get the desired results:

 

xifeng_L_1-1742890032933.png

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

I am not able to create calculated column. The only option I have is to create calculate measure

Since the same items are automatically merged in the matrix, you have to add the calculated columns, and just using the metrics won't give you the effect you're looking for.

So is there any workaround to have the logic that you provided can be done in calculated measure. As I don't have option to create calculated column in my dataset. And we can't use earlier function function in measure

Ankita26_1-1742967362921.png

 

This is not just a matter of switching to measures, because of the auto aggregation nature of the matrix, you have to add columns to the table to achieve what you need.

 

Based on your screenshot, your model should be directly using the dataset on PowerBI Server, you can switch to DirectQuery by clicking on the option in the bottom right corner, which will can create the calculated columns.

 

xifeng_L_0-1742976578826.png

 

If that method doesn't work either, then you may need to contact the Owner of that dataset to get help adding those two calculated columns.

Thanks alot! I am able to ge the expected result.

Thanks for providing the solution but I am not able to use earlier function here. 

Ankita26_0-1742900075771.png

 

Not sure about the scenario of your error, you can refer to my pbix file

Greg_Deckler
Super User
Super User

@Ankita26 Take a look at Cthulhu and Streaks because I'm pretty sure the solution starts there. 

https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/Streaks/td-p/4051984

https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/Cthulhu/m-p/509739#M211

 



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...

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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