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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
v-rongtiep-msft
Community Support
Community Support

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
v-rongtiep-msft
Community Support
Community Support

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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors