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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
MichaelG1117
Helper I
Helper I

Creating a Calculated Column when Making a Table

Hi Everyone, 

I'm making a Date Table and in it I want to include a calculated Column "Total Order Amount" from 'Orders' Table where "Total Order Amount" column equals Orders[OrderQuantity] * Orders[SalesPrice]: 

 

Capture9.PNG

 I used this script to create the Date Table (with the extra calculated column): 

Capture10.PNG

But when I do this the "Total Order Amount" Values on the new table are just the whole of all orders combined: 

 

Capture11.PNG

 

Any way to fix this so the "Total Order Amount" is [OrderQuantity] * [OrderSales] for each particular date instead of the grand total? 

 

Any help would be greatly appreciated. 

 

Thanks----

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

Hi @MichaelG1117 , 

 

Based on your description, Please try code as below to create a Calculated Column.

My Sample:

vweiyan1msft_0-1703066368988.png

Table = 
var _table=
ADDCOLUMNS(
    CALENDAR(DATE(2013,1,1),DATE(2013,1,31)),
    "Weeknum", WEEKNUM([Date]),
    "Week Number & Year", "W" & WEEKNUM([Date]) & " " &YEAR([Date]),
    "WeekYearNumber", YEAR([Date]) & 100 + WEEKNUM([Date])
    )
return
ADDCOLUMNS(
    _table,    
    "Total Order Amount",
    SUMX(
    FILTER(ALL(Orders),
    'Orders'[OrdeDaterKey]=EARLIER([Date])),[OrderQuantity]*[SalesPrice])
    )

Result is as below.

vweiyan1msft_1-1703066528265.png

 

Best Regards,
Yulia Yan

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

7 REPLIES 7
v-weiyan1-msft
Community Support
Community Support

Hi @MichaelG1117 , 

 

Based on your description, Please try code as below to create a Calculated Column.

My Sample:

vweiyan1msft_0-1703066368988.png

Table = 
var _table=
ADDCOLUMNS(
    CALENDAR(DATE(2013,1,1),DATE(2013,1,31)),
    "Weeknum", WEEKNUM([Date]),
    "Week Number & Year", "W" & WEEKNUM([Date]) & " " &YEAR([Date]),
    "WeekYearNumber", YEAR([Date]) & 100 + WEEKNUM([Date])
    )
return
ADDCOLUMNS(
    _table,    
    "Total Order Amount",
    SUMX(
    FILTER(ALL(Orders),
    'Orders'[OrdeDaterKey]=EARLIER([Date])),[OrderQuantity]*[SalesPrice])
    )

Result is as below.

vweiyan1msft_1-1703066528265.png

 

Best Regards,
Yulia Yan

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

Thanks!

Greg_Deckler
Super User
Super User

@MichaelG1117 Maybe try using RELATEDTABLE in your SUMX although it may not work as the relationship may not technically exist yet as the table is being calculated (created). Could also explore dropping the CALCULATE.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I tried taking out the SumX, did not work. 

 

Tried Relatedtable but was unsure what the script would be --- didn't work

@MichaelG1117 Keep the SUMX. Drop the CALCULATE. Use RELATED or RELATEDTABLE as the first argument for your SUMX


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Dangar332
Super User
Super User

HI, @MichaelG1117 

I think it is correct because there are no filter part present in table from where total order amount column filter means your column(total order amount) has no data lineage for that table so you have to provide some filter part like product code or product name or orderquantity(as above in your first image) or something from which your column(total order amount) filter according to that

I tried:  
 
"Total Order Amount", CALCULATE(Sumx('Orders','Orders'[OrderQuantity]*'Orders'[SalesPrice]),
     FILTER(Orders, Orders[OrderDateKey]))
 
Did not work, though

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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