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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
JaipalAnu
Helper III
Helper III

Convert the DAX Query to Power Query to create Custom Column

Hi Everyone,

Please Help me to Convert the DAX Query to Power Query to create Custom Column. Below is the Query.

The below Dax Query is working well as expected in the Desktop, for few reports the column is needed from power query. So I'm trying to convert it in Power Query but getting error. Pasting the Power query also which i tried.

Dax Query
------------

YTD=
VAR selectMonth = MAX('CostPlans_Monthly'[Reporting_MonthNumber])
RETURN
CALCULATE(
    SUM('CostPlans_Monthly'[Value]),
    FILTER(
        ALL('CostPlans_Monthly'),
        'CostPlans_Monthly'[CostPlanName] = SELECTEDVALUE('CostPlans_Monthly'[CostPlanName]) &&
        'CostPlans_Monthly'[Reporting_MonthNumber] <= selectMonth &&
        ('CostPlans_Monthly'[PlanType] = "Actual" || 'CostPlans_Monthly'[PlanType] = "Plan")
    )
)

Power Query
----------------

let
Source = CostPlans_Monthly2, 
SelectMonth = List.Max(Source[Reporting_MonthNumber]),
FilteredTable = Table.SelectRows(
Source,
each [CostPlanName] = Table.SingleValue(Source[CostPlanName])
and [Reporting_MonthNumber] <= SelectMonth
and ([PlanType] = "Actual" or [PlanType] = "Plan")
),
YTDTotal = List.Sum(FilteredTable[Value])
in
YTDTotal


Please help me to fix this


1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @JaipalAnu 

You can create a blank query and put the following code to advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJMLilNzAEyjAyMjPUN9Q2BTEMDpVgdiHRATmIeTNJI3xTMxCppDNZpjJD0L8lILUKVNYHIOmE11xAhieomiF4j7HpRrHVCt9YI2VpnNK0ofnXGsBbFt+h6EdbGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CostPlanName = _t, PlanType = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CostPlanName", type text}, {"PlanType", type text}, {"Date", type date}, {"Value", Int64.Type}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
    #"Inserted Month" = Table.AddColumn(#"Inserted Year", "Reporting_MonthNumber", each Date.Month([Date]), Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Inserted Month", "Sum_value", each List.Sum(Table.SelectRows(#"Inserted Month",(x)=>(x[PlanType]="Actual" or x[PlanType]="Plan") and x[CostPlanName]=[CostPlanName] and x[Year]=[Year] and x[Reporting_MonthNumber]<=[Reporting_MonthNumber])[Value])),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"CostPlanName", Order.Ascending}, {"Date", Order.Ascending}})
in
    #"Sorted Rows"

Best Regards!

Yolo Zhu

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

11 REPLIES 11
wdx223_Daniel
Super User
Super User

let
Source = CostPlans_Monthly2, 

Custom1=Table.Group(

                                     CostPlans_Monthly2,

                                     "CostPlanName",

                                     {"n",each let a=Table.Group(

                                                                                  _,

                                                                                 { "CostPlanName","Reporting_MonthNumber"},

                                                                                 {"MonthTotal",each List.Sum(Table.SelectRows(_,each [PlanType]="Actual" or [PlanType]="Plan")[Value])}

                                                                                  ),

                                                           b=List.Accumulate(a[MonthTotal],{},(x,y)=>x&{List.Last(x,0)+y})

                                                    in Table.FromColumns(Table.ToColumns(a)&{b},Table.ColumnNames(a)&{"YTD"})

                                       }

                                     ),

Custom2=Table.Combine(Custom1[n])

in

Custom2

Thanks for your reply, I have tried with the Query you provided but getting error.

what's the error?

 

After Custom Column Created getting error like below. Attached Image.

JaipalAnu_0-1699339468311.png

 

what's the message in the error?

There is no message, The error is showing for all the rows after custom column created.

JaipalAnu_0-1699339911363.png

 

you can select any cell, then the error message will be show under the table

The error is below

JaipalAnu_0-1699341949515.png

 

try this code

Source = Table.Buffer(CostPlans_Monthly2), 

Custom1=Table.Group(

                                     Source,

                                     "CostPlanName",

                                     {"n",each let a=Table.Group(

                                                                                  _,

                                                                                 { "CostPlanName","Reporting_MonthNumber"},

                                                                                 {"MonthTotal",each List.Sum(Table.SelectRows(_,each [PlanType]="Actual" or [PlanType]="Plan")[Value])}

                                                                                  ),

                                                           b=List.Accumulate(a[MonthTotal],{},(x,y)=>x&{List.Last(x,0)+y})

                                                    in Table.FromColumns(Table.ToColumns(a)&{b},Table.ColumnNames(a)&{"YTD"})

                                       }

                                     ),

Custom2=Table.Combine(Custom1[n])

in

Custom2

No Luck, Same error. 

Anyhow, Thanks for your time.

Anonymous
Not applicable

Hi @JaipalAnu 

You can create a blank query and put the following code to advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJMLilNzAEyjAyMjPUN9Q2BTEMDpVgdiHRATmIeTNJI3xTMxCppDNZpjJD0L8lILUKVNYHIOmE11xAhieomiF4j7HpRrHVCt9YI2VpnNK0ofnXGsBbFt+h6EdbGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CostPlanName = _t, PlanType = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CostPlanName", type text}, {"PlanType", type text}, {"Date", type date}, {"Value", Int64.Type}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
    #"Inserted Month" = Table.AddColumn(#"Inserted Year", "Reporting_MonthNumber", each Date.Month([Date]), Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Inserted Month", "Sum_value", each List.Sum(Table.SelectRows(#"Inserted Month",(x)=>(x[PlanType]="Actual" or x[PlanType]="Plan") and x[CostPlanName]=[CostPlanName] and x[Year]=[Year] and x[Reporting_MonthNumber]<=[Reporting_MonthNumber])[Value])),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"CostPlanName", Order.Ascending}, {"Date", Order.Ascending}})
in
    #"Sorted Rows"

Best Regards!

Yolo Zhu

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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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 Kudoed Authors