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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
coatsy35
Helper I
Helper I

DAX formula to include count from certain months onwards in a previous year and add to YTD

I have the following problem, I have a large spreadsheet with counts of items sold YTD - year selected using a Year slicer. This all works fine. However, the recording window for one item has been changed to now be 1st July until 31st June instead of 1st Jan - 31st Dec like all the other sales.

 

How would I alter my DAX count code to look backwards and count not just this year as given by the slicer, but also count any Bus's sold from the 1st of the 7th month in the previous year? Sample File 

 

Screenshot 2024-02-22 143823.png

 

 

1 ACCEPTED SOLUTION
amustafa
Super User
Super User

Here's a DAX to account for your fisca year for Type = 'Bus'.

Adjusted Count of Unit Type = 
VAR CurrentYearStart = DATE(YEAR(TODAY()), 1, 1)
VAR CurrentYearEnd = DATE(YEAR(TODAY()), 12, 31)
RETURN
SUMX(
    'Table1',
    VAR SaleDate = 'Table1'[Sale Date]
    VAR SaleYear = YEAR(SaleDate)
    VAR SaleMonth = MONTH(SaleDate)
    VAR IsBus = 'Table1'[Unit Type] = "Bus"
    VAR AdjustedSaleDate = IF(IsBus && SaleMonth >= 7, DATE(SaleYear + 1, SaleMonth, DAY(SaleDate)), SaleDate)
    RETURN
        IF(AdjustedSaleDate >= CurrentYearStart && AdjustedSaleDate <= CurrentYearEnd, 1, 0)
)

 

amustafa_0-1708618100143.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
amustafa
Super User
Super User

See my Sample file and observe how you placed the newly calculated column in Values and Type as columns.

DAX formula to include count from certain months.xlsx





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi,

 

Obviously way more complicated than I thought. I have downloaded your file but the values are still not showing how I need them. Please see screenshot below as to what it should be showing when the slicer is selected to 2023 or 2024. Sorry I may have not been very clear. Thank you for your help so far, I've learnt quite a bit from this already. Still not sure how to get what I need though, if I don't select a year in the slicer the grand total row does give me the values for this year. However the table will be filtered by users to show results for different years, just the "Bus" total for each year needs to be offset backwards 6 months and stop forwards 6 months fo each year, as it has a different fiscal year to the rest of the values. 

 

Screenshot 2024-02-23 091652.png

I had hoped to do it with pure dax, but I've found another way to do it by adding in a helper column of which year the figures belong to. see below

 

Screenshot 2024-02-23 105732.png

 

coatsy35
Helper I
Helper I

Thank you for that. Howver, I copied this into my sample file to try it, all it does is add up everything in 2024 to it says 6 buses were sold. Not sure whats not right. It was a direct copy and paste into a new measure, I've obviosuly done something wrong. See image below

 

Screenshot 2024-02-22 173729.png

amustafa
Super User
Super User

Here's a DAX to account for your fisca year for Type = 'Bus'.

Adjusted Count of Unit Type = 
VAR CurrentYearStart = DATE(YEAR(TODAY()), 1, 1)
VAR CurrentYearEnd = DATE(YEAR(TODAY()), 12, 31)
RETURN
SUMX(
    'Table1',
    VAR SaleDate = 'Table1'[Sale Date]
    VAR SaleYear = YEAR(SaleDate)
    VAR SaleMonth = MONTH(SaleDate)
    VAR IsBus = 'Table1'[Unit Type] = "Bus"
    VAR AdjustedSaleDate = IF(IsBus && SaleMonth >= 7, DATE(SaleYear + 1, SaleMonth, DAY(SaleDate)), SaleDate)
    RETURN
        IF(AdjustedSaleDate >= CurrentYearStart && AdjustedSaleDate <= CurrentYearEnd, 1, 0)
)

 

amustafa_0-1708618100143.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.