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
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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.