I want to calculate a DAX measure "without" the current row. Here is an example based on AdventureWorksDW2014.
evaluate
summarize(
'FactInternetSales'
, 'DimProduct'[ProductSubcategoryName]
Cell-level measures, these are defined as in https://learn.microsoft.com/en-us/sql/analysis-services/lesson-5-create-calculated-columns
, "Margin", [InternetTotalMargin] -- InternetTotalMargin := SUM([Margin])
, "Cost", [InternetTotalProductCost] -- InternetTotalProductCost := SUM([TotalProductCost])
I defined a new measure which is a composite (ratio) of existing measures:
, "Gross Margin", [GrossMargin] -- GrossMargin := DIVIDE([InternetTotalMargin], [InternetTotalProductCost])
Column totals:
, "Total Margin", calculate([InternetTotalMargin], allselected('DimProduct'))
, "Total Cost", calculate([InternetTotalProductCost], allselected('DimProduct'))
Column totals without current row, obtained by subtracting the cell value expression from the column total value expression:
, "Total Margin w/o Subcat", calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin]
, "Total Cost w/o Subcat", calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost]
This does all the aggregations before applying the formula:
, "Total Gross Margin", calculate([GrossMargin], allselected('DimProduct'))
I want to calculate "Total Gross Margin" without the "current row".
My first attempt yields the desired result but depends on explicit knowledge of the formula behind GrossMargin.
This is "explicit" because we essentially re-implement the calculation after subtracting the "current row" out of the numerator and denominator.
I want to be able to do this with measures where I don't know the formula, or the formula is potentially more complex than simple division.
, "Total Gross Margin w/o Subcat (Explicit)",
-- explicitly repeat the division formula
divide(
-- explicitly recompute the numerator
calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin]
-- explicitly recompute the denominator
, calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost]
)
-- next step/where this is meant to be used:
, "Gross Margin Impact (Explicit)", calculate([GrossMargin], allselected('DimProduct')) - divide(calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin], calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost])
I want a more general solution that does not require knowledge of the calculation, something like this:
/*
, "Gross Margin w/o Subcat", calculate([GrossMargin], allselected('DimProduct' Except Current Row)
, "Gross Margin Impact, calculate([GrossMargin], allselected('DimProduct')) - calculate([GrossMargin], allselected('DimProduct' Except Current Row)
*/
Different failed attempts (using Margin rather than GrossMargin since it is easier to check):
, "Trial 1", calculate([InternetTotalMargin], except(all('DimProduct'), 'DimProduct')) -- incorrect value
, "Trial 2", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductSubcategoryName])) -- incorrect value
, "Trial 3", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductCategoryName])) -- incorrect value
, "Trial 4", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductKey])) -- incorrect value
, "Trial 5", calculate([InternetTotalMargin], 'DimProduct') -- incorrect
, "Trial 6", calculate([InternetTotalMargin], 'DimProduct'[ProductSubcategoryName] <> 'DimProduct'[ProductSubcategoryName]) -- illogical, and blank result
-- , "Trial 7", calculate([InternetTotalMargin], 'DimProduct'[ProductSubcategoryName] <> [ProductSubcategoryName]) -- invalid syntax
-- running out of ideas here
)
;
Is this even possible in DAX?
No, this is not possible in DAX.
If you want to calculate a measure you need to define it first. You can't create a place holder/insert formula here/ and hope for the best.
What you can do is use the keyword VAR to define your measure, calculate different measures at the time of calculation and pick the one that suits best. Alternatively and if possible, use the calling function to define the measure by a "replace" method.
Related
I am trying to create a measure that would count the instances when another measure is between given values.
The first is a measure of forecast accuracy, which is calculated over products and customers with a target value of 1. Then I would like to make a monthly report which shows for how many products the forecast accuracy is less than .85, between 0.85 and 1.15 and over 1.15.
The measure I tried for the middle category, which does not give the desired result:
var tab = SUMMARIZE(data, data[ComponentNumber], "Accuracy", [Forecast accuracy])
return SUMX(tab, IF([Accuracy] > 0.85 && [Accuracy] < 1.15, 1, 0))
The data table has also a customer number, which is why I tried first evaluating the measure [Forecast accuracy] only over components, disregarding the customers.
One source of the problem may lie in the fact that the measure [Forecast accuracy] is calculated as a division of two measures [Ordered Quantity] and [Forecast Quantity], of which the former is in another table. Does this affect the evaluation of my attempted measure?
I want to have the value of "ToTal Operation Income/(Loss)" by
subtracting the total value of "Other Operating Income" to "GROSS PROFIT/(LOSS)".
I don't know how to do it. The fact that "Revenue", "LESS:COST OF SALES" AND "Other Operating Income" is grouping.
I have a list of products and would like to get a 50 day simple moving average of its volume using Power Query (M).
The table is sorted by product name and date. I add a custom column and applied the code below.
if [date] >= #date(2018,1,29)
then List.Average(List.Range(Source[Volume],[Volume]-1,-50))
else ""
Since it is already sorted by date and name, an if statement was applied with a date as criteria/filter. However, an error occurs that says
'Volume' column not found in the table.
I expect to have an added column in the power query with volume 50 day moving average per product. the calculation to be done if date is greater than or equal Jan 29, 2018.
We don't know what your columns are, but assuming you have [product], [date] and [volume] in Source, this would average the last 50 days of [volume] for the identical [product] based on each [date], and place in a new column
AvgAmountAdded = Table.AddColumn(Source, "AverageAmount", (i) => List.Average(Table.SelectRows(Source, each ([product] = i[product] and [date]<=i[date] and [date]>=Date.AddDays(i[date],-50)))[volume]), type number)
Finally! found a solution.
First, apply Index by product see this post for further details
Then index again without criteria (index all rows)
Then, apply below code
= Table.AddColumn(#"Previous Step", "Volume SMA(50)", each if [Index_byProduct] >= 50 then List.Average(List.Range(#"Previous Step"[Volume], ([Index_All]-50),50)) else 0),
For large dataset, Table.Buffer function is recommended after index-expand step to improve PQ calculation speed
I currently have a saved search that populates a list of items.
My current results are standard NetSuite fields which are "Name", "Description", "Type", "Average Cost" & "Available"
I am trying to add another column for a formula that multiplies the Average Cost by the Available to give me the Value of the Available SOH.
In your saved search results add a new field of type formula(numeric). In the formula popup window use this formula:
NVL({averagecost}, 0) * NVL({quantityavailable}, 0)
This will multiply the average cost and quantity available together and give you the result. I put the NVL in there in case an item doesn't have an average cost or quantity available you won't get an error.
I'm combining reports to create a single general ledger report, currently it consists of 84 seperate reports. My idea is the end user will have a drop down for the department and month. These are my columns:
Account Number Account Description Current Period Actual
YTD Actual YTD Budget YTD Budget Variance
Total YR Budget Account Status
I have most of it figured out, but can't understand how to figure YTD Actual and YTD Budget since these will require a Sum of multiple Fields depending on what month and department is selected.
My where statement goes something like this and takes care of the current period actual and account number:
Where
( gl_master.acct_cde = gl_master_comp_v.acct_cde ) and
( gl_master.budget_officer = budget_off_mstr.budget_officer ) and
( ( gl_master_comp_v.acct_comp1 = '01' ) AND
( budget_off_mstr.budget_officer IN (#BudgetOfficer) ) ) AND
((#Month = 1 AND gl_master.post_bal_mon_1) OR
(#Month = 2 AND gl_master.post_bal_mon_2)...
How can I have the query recognize what needs to be put into the column when there are multiple fields being summed.
Thanks for any insight. If you made something like this before a small sample of it would be very helpful.
I worked it out.
It needs to be done in a calculated field within the dataset. Then a sum can be done on the field in the textbox.
small chunk of calculated field:
=Cdec(Switch(Parameters!Month.Value = 1, Fields!post_bal_mon_1.Value,
Parameters!Month.Value = 2, Fields!post_bal_mon_2.Value + Fields!post_bal_mon_1.Value,
Parameters!Month.Value = 3, Fields!post_bal_mon_3.Value + Fields!post_bal_mon_1.Value + Fields!
post_bal_mon_2.Value,
Parameters!Month.Value = 4, Fields!post_bal_mon_4.Value + Fields!post_bal_mon_1.Value + Fields!
post_bal_mon_2.Value + Fields!post_bal_mon_3.Value,...))
textbox for sum(I place this in footer):
=SUM(Fields!post_bal_mon_1.value, "DataSet1")