I got a table with a field called PERCENT. it holds the % value for each item category.
Then, I have another calculation parameter to calculate the percentage of QTy of "SD" out of Total Qty.
Now I need to add a new column to show the difference between PERCENT - the percentage of QTy.
I tried below DAX,
NEW2 =
VAR V1 =
CALCULATE(
SUM(PO_POOPEND[ORDER_QTY]),
FILTER(
AP_APCATGRY,
AP_APCATGRY[APCATGRY CODE] = "LC"
)
)
VAR V2 =
CALCULATE(
SUM(PO_POOPEND[ORDER_QTY])
)
VAR v3 = calculate (V1/V2 *100)
var v4 = SUMX(AP_MATCAT,AP_MATCAT[PERCENT] - [NEW])
VAR RESULT = v4
RETURN
RESULT
is it correct? Can someone help me with this
Your DAX is unnecessarily complicated. Since you're not making use of any of the temporary variables v1, v2 or v3 it can be simplified to:
NEW2 =
SUMX (
AP_MATCAT,
AP_MATCAT[PERCENT] - [NEW]
)
But where does measure [NEW] come from?
Related
I have created the following code in Power BI to give me LY sales figures for any selected range.
Total Net Sales LY =
VAR MINSequentialDayNumber = MIN('Dim_Date'[SequentialDayNumber-LY])
VAR MAXSequentialDayNumber = MAX('Dim_Date'[SequentialDayNumber-LY])
RETURN
SUMX(
FILTER(
ALL(Dim_Date),
'Dim_Date'[SequentialDayNumber]>=MINSequentialDayNumber && 'Dim_Date'[SequentialDayNumber]<=MAXSequentialDayNumber),[Total Net Sales])
I am trying to do the same for a Count of Distinct Location IDs but DISTINCTCOUNTX is not usable.
Is there a way to do this with DAX, otherwise I will have to create a separate table of distinct counts and feed in which is getting messy.
Location Count LY =
VAR MINSequentialDayNumber = MIN('Dim_Date'[SequentialDayNumber-LY])
VAR MAXSequentialDayNumber = MAX('Dim_Date'[SequentialDayNumber-LY])
RETURN
DISTINCTCOUNTX(
FILTER(
ALL(Dim_Date),
'Dim_Date'[SequentialDayNumber]>=MINSequentialDayNumber && 'Dim_Date'[SequentialDayNumber]<=MAXSequentialDayNumber),[DistinctTransaction_ID])
OK I think I have worked it out...
Location Count LY =
VAR MINSequentialDayNumber = MIN('Dim_Date'[SequentialDayNumber-LY])
VAR MAXSequentialDayNumber = MAX('Dim_Date'[SequentialDayNumber-LY])
RETURN
CALCULATE
(DISTINCTCOUNT('Fact_Sales'[DistinctTransaction_ID]),
FILTER(ALL(Dim_Date),
'Dim_Date'[SequentialDayNumber]>=MINSequentialDayNumber && 'Dim_Date'[SequentialDayNumber]<=MAXSequentialDayNumber)
)
In my sales table I would like to change cumulative values into single values. Here is sample data from my table.
I created a measure that as far as I know should works for this.
sales_single_values = VAR current_sales = SUM('sales'[sales cumulative]) VAR prev_sales SUM('sales'[sales cumulative]) - CALCULATE( current_sales, 'sales'[period] = 'sales'[period] - 1) Return IF(ISBLANK(prev_sales), BLANK(), current_sales - prev_sales)
But unfortunately the final result on the chart is still the same as I used cumulative values, not single ones. Any ideas what should I change in my measure?
Expected values would be:
Period 1: 4
Period 2: 2
Period 3: 7
As a measure, something like:
sales_single_values =
var prev = max('sales'[period]) - 1
var prev_sales = CALCULATE( SUM('sales'[sales cumulative]), 'sales'[period] = prev)
Return
sum(sales[sales cumulative]) - prev_sales
But this seems like more of a modeling transformation, so do it in PQ, or with a calculated column, like
current sales =
var prev = calculate( max('sales'[period]) ) - 1
var prevSales = calculate( sum(sales[sales cumulative]), all(sales), sales[period] = prev)
return
sales[sales cumulative]-prevSales
Need some help to calculate the following in two separate columns in a DAX formula
Earliest Reading for each equip
Latest Reading for each equip
Screenshot of sheet
I've been able to get the first date of each equip with this.
CALCULATE(FIRSTDATE(Transactions[Date]),ALLEXCEPT(Transactions,Transactions[Equip No]))
But cannot work out how to get the 'Reading' value that is associated with the first date
I've managed to do this with a measure, but would also like to get this in a calc. column.
Latest Reading =
SUMX (
VALUES( Transactions[Equip No] ),
CALCULATE ( MIN ( Transactions[Reading] ), FIRSTDATE ( Transactions[Date] ) )
)
this should help you.
Earliest Column
Earliest =
VAR __equipNumber = 'Transaction'[Equip No] //Get the Equip No to filter the main table and create an auxiliar table for every different Equip No.
VAR __minDate = CALCULATE( MIN('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) ) //Get the lowest date asociated to every Equip No.
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __minDate ) //Create a table that contains 1 row asociate to the lowest date.
Return CALCULATE(SUM('Transaction'[Reading]), __subTable) //Operate over the auxiliar table to get the expected value.
Latest Column
Latest =
VAR __equipNumber = 'Transaction'[Equip No]
VAR __maxDate = CALCULATE( MAX('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) )
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __maxDate )
Return CALCULATE(SUM('Transaction'[Reading]), __subTable)
I obtained the expected result
I'm trying to build a measure that calculates the weighted average of some KPIs, but all the data are in a single column.
This is the kind of data I have:
Line Indicator Value
A Efficiency 90
B Efficiency 80
A Weight 5
B Weight 10
And this measure should return me 83.33, from (90 * 5 + 80 * 10)/(5 + 10).
How can I use DAX to get this result? Or should I use some other method?
Thank you!
I pivoted the Indicator column as told by #AlexisOlson, and then calculated the weighted averages using simple SUMX syntax. Solved.
This might help you.
First, create a Calculated Column using this DAX Formula:
Column =
VAR line = 'Table'[Line]
VAR SubTable = FILTER( 'Table'; 'Table'[Line] = line )
VAR eff = CALCULATE( SUM('Table'[Value]); 'Table'[Indicator] = "Efficiency"; SubTable )
VAR w = CALCULATE( SUM('Table'[Value]); 'Table'[Indicator] = "Weight"; SubTable )
Return IF( 'Table'[Indicator] = "Efficiency"; eff * w )
Then you have to create a measure like this:
Result =
VAR SumColumn = SUM( 'Table'[Column] )
VAR TotalWeight = CALCULATE( SUM( 'Table'[Value] ); 'Table'[Indicator] = "Weight" )
Return DIVIDE( SumColumn; TotalWeight; BLANK() )
Hope that helps.
Please see the file I have shared in the following link:
https://drive.google.com/file/d/1q7FE85qHoB_OhAm4hlmFidh6WP3m-UnK/view?usp=sharing
I have a dataset with the value of various items on different dates. I first need to calculate ratio by dividing the value of an item on a particular date with the earliest value in the chosen date filters. I then need to show the per day ratio of all items (columns I & J) as the desired output.
I am able to get the "first value" in the filtered table by using the following formula:
first_cr =
VAR item = MAX('Table 1'[item])
VAR min_updated = CALCULATE(
MIN('Table 1'[last_updated]),
'Table 1'[item] = item,
ALLSELECTED('Table 1')
)
RETURN
CALCULATE(
AVERAGE('Table 1'[cr]),
'Table 1'[last_updated] = min_updated,
'Table 1'[item] = item,
ALLSELECTED('Table 1'[fk])
)
However, I am unable to figure out how to calculate the average of all the individual item ratios at just the date level. I guess I need to use AVERAGEX somehow, but not sure how. Perhaps my first_cr formula could use more refinement.
I'd appreciate any help or guidance in the right direction. Thanks for your time.
I was able to get the answer using the following formula. If anyone can refine it better, please do so, else I'll accept my answer after a few days.
ret =
var lastUpdated = MAX(Sheet1[Date])
var tbl = ALLSELECTED(Sheet1)
RETURN
CALCULATE(
AVERAGEX(
Sheet1,
var i = Sheet1[Item]
var minUpdated = CALCULATE(
MIN(Sheet1[Date]),
Sheet1[Item] = i,
tbl
)
var first_cr = CALCULATE(
AVERAGE(Sheet1[Return]),
Sheet1[Date] = minUpdated,
Sheet1[Item] = i,
tbl
)
RETURN Sheet1[Return] / first_cr
),
Sheet1[Date] = lastUpdated,
tbl
)