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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
lilskyy4202
Regular Visitor

how to transform data in year and month

Hi all

I have a data sheet generate from system in fiscal year and the header are without the year.

For example:

April to Dec = 2021

Jan to Mar = 2022

tableA.JPG

 i tried the following formula, but it does not seem to work.

if Text.Middle([Attribute], 3, 3) = "Jan" or Text.Middle([Attribute], 3, 3) = "Feb" or Text.Middle([Attribute], 3, 3) = "Mar"
then [Attribute] & "-2022"
else [Attribute] & "-2021"

 

Is there any way to transform this data. I am new to this powerbi and hoping to learn something new here 🙂

Thanks for your help

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @lilskyy4202 

 

Since you're using the attribute column, I assume you have already unpivoted your data. Try the following

let 
// converting to mmm-yy format so Power Query can parse it as a date with Date.From
mmmyy = Date.From([Attribute] & "-24"),
//get the month number
MonthNumber = Date.Month(mmmyy),
//get the year 
yr = if MonthNumber >=4 then 2021 else 2022
in [Attribute] & "-" & Text.From(yr)

danextian_1-1734252880369.png

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
Bibiano_Geraldo
Super User
Super User

Hi @lilskyy4202 ,

First to work with your data in power bi, you need to Unpivot the columns in power query:

 

1- Select the column with A,B,C rows and then choose unpivot other columns:

Bibiano_Geraldo_0-1734260590576.png

 

Now your data should look like this:

Bibiano_Geraldo_1-1734260684377.png

 

2- Add custom column and paste the following M code:

Bibiano_Geraldo_2-1734260978368.png

let
    CurrentYear = 2022
in
    if Date.Month(Date.FromText("01 " & [Month] & " 2000")) >= 4 
    then #date(CurrentYear-1, Date.Month(Date.FromText("01 " & [Month] & " 2000")), 1)
    else #date(CurrentYear, Date.Month(Date.FromText("01 " & [Month] & " 2000")), 1)

 

4- Change the column data type for Date:

Bibiano_Geraldo_3-1734261078237.png

 

 

danextian
Super User
Super User

Hi @lilskyy4202 

 

Since you're using the attribute column, I assume you have already unpivoted your data. Try the following

let 
// converting to mmm-yy format so Power Query can parse it as a date with Date.From
mmmyy = Date.From([Attribute] & "-24"),
//get the month number
MonthNumber = Date.Month(mmmyy),
//get the year 
yr = if MonthNumber >=4 then 2021 else 2022
in [Attribute] & "-" & Text.From(yr)

danextian_1-1734252880369.png

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Thank you so much, It works!

Helpful resources

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

Top Solution Authors