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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
GustavoKamchen
Frequent Visitor

Year and Month in the same graphic

Hello.

 

I am trying to make a graphic like the one below, with last year, current year, and the last 5 months (April - 4, May - 5, June - 6, July - 7, August - 😎 in the same graphic. How can I achieve this using DAX?

 

Note: No sensible data were given. These values are AVERAGE per month.

 

GustavoKamchen_0-1661196061159.png

 

My data:

YearMonthSales
20211 R$   31.371,81
20212 R$   27.398,73
20213 R$   30.517,70
20214 R$   29.716,61
20215 R$   31.885,99
20216 R$   29.088,88
20217 R$   31.284,70
20218 R$   31.291,78
20219 R$   30.507,82
202110 R$   31.571,20
202111 R$   29.567,97
202112 R$   31.036,40
20221 R$   31.458,14
20222 R$   27.969,00
20223 R$   30.223,21
20224 R$   29.986,37
20225 R$   32.006,22
20226 R$   29.293,11
20227 R$   31.637,75
20228 R$   22.377,59

 

Data for graphic:

YearSales
2021 R$   30.436,63
2022 R$   29.368,92
4 R$   29.986,37
5 R$   32.006,22
6 R$   29.293,11
7 R$   31.637,75
8 R$   22.377,59
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @GustavoKamchen ,

Please refer to my pbix file to see if it helps you.

Create 2 columns.

sales value =
VAR _monthtoday =
    MONTH ( TODAY () )
VAR _yeartoday =
    YEAR ( TODAY () )
VAR _qian5 =
    MONTH ( EOMONTH ( TODAY (), -5 ) )
VAR _aaverage =
    CALCULATE (
        AVERAGE ( 'Table'[Sales] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Year] = EARLIER ( 'Table'[Year] ) )
    )
RETURN
    IF (
        ( 'Table'[Year] ) = _yeartoday
            && ( 'Table'[Month] ) > _qian5,
        ( 'Table'[Sales] ),
        _aaverage
    )
Columnyear =
VAR _1 =
    CALCULATE (
        COUNT ( 'Table'[sales value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Year] = EARLIER ( 'Table'[Year] )
                && 'Table'[sales value] = EARLIER ( 'Table'[sales value] )
        )
    )
VAR _2 = 'Table'[Year]
RETURN
    IF (
        _1 = 1,
        ( 'Table'[Month] ),
        IF ( _1 > 1 && ( 'Table'[Year] ) = _2, ( 'Table'[Year] ), 0 )
    )

 

vpollymsft_0-1661417755528.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @GustavoKamchen ,

Please refer to my pbix file to see if it helps you.

Create 2 columns.

sales value =
VAR _monthtoday =
    MONTH ( TODAY () )
VAR _yeartoday =
    YEAR ( TODAY () )
VAR _qian5 =
    MONTH ( EOMONTH ( TODAY (), -5 ) )
VAR _aaverage =
    CALCULATE (
        AVERAGE ( 'Table'[Sales] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Year] = EARLIER ( 'Table'[Year] ) )
    )
RETURN
    IF (
        ( 'Table'[Year] ) = _yeartoday
            && ( 'Table'[Month] ) > _qian5,
        ( 'Table'[Sales] ),
        _aaverage
    )
Columnyear =
VAR _1 =
    CALCULATE (
        COUNT ( 'Table'[sales value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Year] = EARLIER ( 'Table'[Year] )
                && 'Table'[sales value] = EARLIER ( 'Table'[sales value] )
        )
    )
VAR _2 = 'Table'[Year]
RETURN
    IF (
        _1 = 1,
        ( 'Table'[Month] ),
        IF ( _1 > 1 && ( 'Table'[Year] ) = _2, ( 'Table'[Year] ), 0 )
    )

 

vpollymsft_0-1661417755528.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@GustavoKamchen , You have create a new column

 

New Year Month = If( year([Date]) < Date(Year(Today()),1,1), Year([Date]) , month([Date]))

 

Sort this on . Select the above column and mark this as sort column

New Year Month Sort = If( year([Date] < Date(Year(Today()),1,1), Year([Date])*100 , Year([Date])*100 + month([Date])

 

How to Create Sort Column and Solve Related Errors:
https://www.youtube.com/watch?v=KK1zu4MBb-c

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.