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
PBI_hlpr1
Resolver I
Resolver I

Adding a custom column in DAX and in PQ

Hi all,
n a static table I am adding a calculated column with the following DAX function:

Check FY = 
// Check if a date is in current or last FY
VAR CurrentFY =
    CALCULATE (
        MAX ( Dim_Dates[FY_Val] ),
        Dim_Dates[Date] = TODAY (),
        ALL ( Dim_Dates )
    )

VAR DifFY = Dim_Scen_Auto[FY_val] - CurrentFY
RETURN
    SWITCH ( TRUE (), DifFY = 0, "Current FY", DifFY = -1, "Last FY", "" )


As you can see it is adding a description if a Fiscal Year is "Current FY" or "Last FY" and behaves exactly as expected. For several reasons I would like to have this column added via PQ rather than via DAX but I am struggling in the conversion.

Is this doable in a simple way?

Many thanks in advance,

C.

1 ACCEPTED SOLUTION
v-tianyich-msft
Community Support
Community Support

Hi @PBI_hlpr1 ,

 

You can try the following expression:

vtianyichmsft_0-1704076505668.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE11DVUitUBc4x1DY10jdG4BjAuSKkRipyRpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year.1", each Date.Year([Year]), Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Inserted Year", "Custom", each let
    CurrentDateTime = DateTime.LocalNow(),
    CurrentYear = Date.Year(CurrentDateTime),
    result = if CurrentYear-[Year.1]=0 then "Current FY" else if CurrentYear-[Year.1]=1 then "Last FY" else ""
    in
    result)
in
    #"Added Custom"

 

An attachment for your reference. Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

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

3 REPLIES 3
v-tianyich-msft
Community Support
Community Support

Hi @PBI_hlpr1 ,

 

You can try the following expression:

vtianyichmsft_0-1704076505668.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE11DVUitUBc4x1DY10jdG4BjAuSKkRipyRpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year.1", each Date.Year([Year]), Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Inserted Year", "Custom", each let
    CurrentDateTime = DateTime.LocalNow(),
    CurrentYear = Date.Year(CurrentDateTime),
    result = if CurrentYear-[Year.1]=0 then "Current FY" else if CurrentYear-[Year.1]=1 then "Last FY" else ""
    in
    result)
in
    #"Added Custom"

 

An attachment for your reference. Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

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

Hi @v-tianyich-msft ,

 

your solution works perfectly and helped a lot pointing me to the solution and to a better understanding of M. As my Fiscal Year does not match Calendar Year (it goes from Dec to Nov) I just had to apply a small change to your code as below:

= Table.AddColumn(#"Added Custom6", "Check FY", each let
        CurrentDateTime = DateTime.LocalNow(),
        CurrentYear = Date.Year(CurrentDateTime)-2000,
        CurrentFY = if Date.Month( CurrentDateTime ) = 12 then CurrentYear+1 else CurrentYear,
        result = if CurrentFY-[FY_val]=0 then "Current FY" else if CurrentFY-[FY_val]=1 then "Last FY" else ""
    in
        result)

 

Thanks, you made my day!

 

Regards,

C.

lbendlin
Super User
Super User

You would have to show the relevant fragment of your Power Query table and indicate what your fiscal year boundaries are.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

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.