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
Niraj_vora0106
Helper III
Helper III

M-Query

Hi community,

Hope you all are doing well!

 

I've a doubt regarding Power BI M-query.

I've created an IF statement via Calculated column, which I'm looking to create in Power Query or Transform Query using M-Query.

 

Below is the IF statement:

Fiscal_Year = IF(Append2[Month]<4"FY" & Append2[Year]-1 &"-"& RIGHT(Append2[Year],4), "FY" & Append2[Year] &"-"& RIGHT(Append2[Year],4) + 1)
output: FY2021-2022
 
I want to create this in backend using M-query.
 
Please help.
11 REPLIES 11
HotChilli
Super User
Super User

You will need to Add a Custom Column in Power Query.

 

It will be a combination of an 'if' statement :

https://gorilla.bi/power-query/if-function-in-power-query/ 

 

and Text.Right

https://learn.microsoft.com/en-us/powerquery-m/text-end 

 

You may need other Text functions:

https://learn.microsoft.com/en-us/powerquery-m/text-functions 

 

because you may need to change some number fields to text so that you can use them.

You can combine text in power query with '&'.

My advice is start it simply (get something basic that works) and build it step by step adding logic

Hi @HotChilli 

 

I tried using M-query text functions.

below is the snip of the code I tried for creating Fiscal Year:

Niraj_vora0106_0-1677784606510.png

 

Please help me out if I'm doing anything wrong in this.

Used Text.From to convert Number to Text. I converted Month and Year datatypes to Text also

 

Still getting error

below is the error snip:

Niraj_vora0106_1-1677784732375.png

 

Please help!

Hi @Padycosmos 

Still getting same error which I mentioned earlier.

 

Niraj_vora0106_0-1677787548314.png

 

Please correct me if I'm wrong

Please delete all that I have scored-off and try again:

Padycosmos_0-1677787953863.png

 

Niraj_vora0106_0-1677789259225.png

Still getting same error.

Niraj_vora0106_1-1677789315526.png

 

 

You may try again after making this change. If it still doesn't work, please share the pbix file, if possible

Padycosmos_0-1677789625492.png

 

Hi @Padycosmos 

I tried but still same error.

 

Really thank you for your help. I'll try finding other ways too

[Year] is a numeric field, therefore, Text.End([Year],4) will not work, instead please try 

Text.End(Number.ToText([Year]),4)

 

Padycosmos_0-1677790813779.png

 

Hi,

I tried doing the way you told 

Below is the snip: for few line items it is working and for few it is not 

Niraj_vora0106_0-1677817354780.png

 

Below is the snip of error

Niraj_vora0106_1-1677817421744.png

 

 

It appears that there is some problem with the data. Could you please change the Data type of the Fiscal Year Column to Whole number and apply the last formula again

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.