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

Quarter calculation help

 

YearQuarterBus NameStop PointDateHrs
2018Q1AmberStart Passage15/03/18123
2018Q1Ambera16/03/1812
2018Q1AmberEnd passage20/03/1814
2018Q1AmberHalt25/03/1832
2018Q1AmberStart Passage27/03/1823
2018Q1Ambere28/03/1856
2018Q2Amberf02/04/1867
2018Q2Amberg03/04/1889
2018Q2AmberEnd passage04/04/181
2018Q2AmberHalt05/04/1814
2018Q2AmberStart Passage06/06/1825
2018Q2Ambera22/06/1834
2018Q2AmberEnd passage24/06/1831
2018Q2AmberStart Passage27/06/1876
2018Q2Ambera28/06/1885
2018Q3Amberb14/07/1864
2018Q3Amberc18/07/1854
2018Q3Amberd22/07/1851
2018Q3Ambere27/07/1835

 

Basically the requirement is to calculate the total hrs, the records which needs to be included in the calculation is with below condition. Let us assume that user selects quarter 2 as a slicer. Now the while calcuating the sum for quarter 2 , measure should include the records as follows - 

Include - the hours wherein bus might have been started in previous quarter but have the end passage in the current quarter i.e. If you see the above table 1st two records highlighted in orange are part of Q1 but as the bus end passage is in Q2 , these values should be part of Q2 hrs

 

Accordingly - consider the entry dated 27/06/2018 onwards wherein the bus has started in Q2 , but it should be excluded from the calculation of Hrs for Quarter for that particular Bus.

 

So if you go through the above statements and compare it with the values that i have highlighted you would be able to link them together.

 

Thanks once again for the kind help

1 ACCEPTED SOLUTION

Hi @Anonymous

 

I would create two additional calculated columns in your table, one with the actual year and one with the actual quarter in which the hours will be counted. Once this is done you can use these columns to slice instead of the Year and Quarter columns you have now.

 

Do note that the code looks for the corresponding "End  passage" row for each fragment and will return a blank when it does not find it. This will happen in the rows of the last passage in your sample data that start in Q2 but have no end. If you want a different behavior the code would have to be modified. It also assumes that "Halt"  is the only event that can appear in the table outside a passage and that the hours of "Halt" have to be counted in whatever in the actual quarter the "Halt" takes place.  Here are the columns:

 

Quarter4HoursCount = 
IF (
    Table1[Stop Point] = "Halt";
    Table1[Quarter];
    VAR _RowWithEndPassageDate =
        TOPN (
            1;
            CALCULATETABLE (
                Table1;
                Table1[Date] >= EARLIER ( Table1[Date] );
                Table1[Stop Point] = "End passage";
                ALLEXCEPT ( Table1; Table1[Bus Name] )
            );
            Table1[Date]; ASC
        )
    RETURN
        CONCATENATEX ( _RowWithEndPassageDate; [Quarter] )
)
Year4HoursCount = 
IF (
    Table1[Stop Point] = "Halt";
    Table1[Year];
    VAR _RowWithEndPassageDate =
        TOPN (
            1;
            CALCULATETABLE (
                Table1;
                Table1[Date] >= EARLIER ( Table1[Date] );
                Table1[Stop Point] = "End passage";
                ALLEXCEPT ( Table1; Table1[Bus Name] )
            );
            Table1[Date]; ASC
        )
    RETURN
        SUMX ( _RowWithEndPassageDate; [Year] )
)

 

Code formatted with   www.daxformatter.com

 

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

For the first part, you should be able to get there using EARLIER. See my article on Mean Time Before Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...

 

For the second part when you exclude it from the quarter, I'm not clear on why it gets excluded.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler - Basically the 2nd part means the following -  

 

As you must have seen in the table i have sent, the End passage of the bus is not completed in Q2 , so it should be the part of Q3 calculation. However this data is updated on day to day basis.

We have to also exclude the records whose end passage data is not present in the current days file. i.e. a scenario wherein Bus started on dec 31st 2018 but its end passage is on 1st feb 2019 but as of today the records that i have is only till 24/01/2019. So all the entries from 31st dec 2018 till today should not be part of any calculation of hrs for Q1 2019. Where as there might be other bus whose end passage is completed within the specified time frame and its hrs sum should be a part of Q1 2019.

 

Just one more information the data that we get is incremental, i.e. on daily basis we get the delta load.

Hi @Anonymous

 

I would create two additional calculated columns in your table, one with the actual year and one with the actual quarter in which the hours will be counted. Once this is done you can use these columns to slice instead of the Year and Quarter columns you have now.

 

Do note that the code looks for the corresponding "End  passage" row for each fragment and will return a blank when it does not find it. This will happen in the rows of the last passage in your sample data that start in Q2 but have no end. If you want a different behavior the code would have to be modified. It also assumes that "Halt"  is the only event that can appear in the table outside a passage and that the hours of "Halt" have to be counted in whatever in the actual quarter the "Halt" takes place.  Here are the columns:

 

Quarter4HoursCount = 
IF (
    Table1[Stop Point] = "Halt";
    Table1[Quarter];
    VAR _RowWithEndPassageDate =
        TOPN (
            1;
            CALCULATETABLE (
                Table1;
                Table1[Date] >= EARLIER ( Table1[Date] );
                Table1[Stop Point] = "End passage";
                ALLEXCEPT ( Table1; Table1[Bus Name] )
            );
            Table1[Date]; ASC
        )
    RETURN
        CONCATENATEX ( _RowWithEndPassageDate; [Quarter] )
)
Year4HoursCount = 
IF (
    Table1[Stop Point] = "Halt";
    Table1[Year];
    VAR _RowWithEndPassageDate =
        TOPN (
            1;
            CALCULATETABLE (
                Table1;
                Table1[Date] >= EARLIER ( Table1[Date] );
                Table1[Stop Point] = "End passage";
                ALLEXCEPT ( Table1; Table1[Bus Name] )
            );
            Table1[Date]; ASC
        )
    RETURN
        SUMX ( _RowWithEndPassageDate; [Year] )
)

 

Code formatted with   www.daxformatter.com

 

Anonymous
Not applicable

@AlB - Thanks alot for the guidance , you have covered the exact scenario which i would require. However just would like to know if at all a bus starts in Dec 2018 i.e. Q4 and ends in 31st Jan 2019 Q1 , would the logic assign the Q1 against such records and if the particular bus has not recahed its end passage what values would be populated in the new quarter and year column for that particular bus records. 

And I believe once on day to day basis i will get additional records for that bus passage and eventually would get the end passage data for the same on 31st Jan 2019, the code will pick up the fragment and would update it to Q1 and year 2019?

@Anonymous

 

Yes, if you look at the code, what it does is look for the next "End passage" to which each line belongs and it will assign the year and and quarter in which the "End passage" takes place. So it should work in your example. It is because of cases like this that I included a new column for the year too, since both would change.

Like I said, the  code will return a blank if it doesn't find the corresponding "End passage". That could be modified but i think it's the most consistent behavior. You can then see the integrity of your data as well. If you refresh the data to include that missing "End passage", the column will be recalculated and will populate those blank rows

But hey, try it out and see for yourself.     

Anonymous
Not applicable

@AlB - The code worked, can you help me with how should i troubleshoot the dax written by you basically i want to get into how each of the query is working

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.