How to convert a recursive excel formula to DAX - dax

I am trying to transfer this excel operation to DAX in PowerBI. An illustration of the problem is shown in Excel screenshot. I am basically calculating the weighted average and placing it in the cell below. I know this same logic cannot be used in DAX. Is there a way to work around it? It is referencing the same column I am defining. I know this is possible in excel and was hoping there could be a workaround in Power BI. In my Power BI works, the columns being referenced are filtered from calculated measures. In the excel sheet, the columns A,B,C, and D are calculated. Here is a link to a .pbix file of this problem. Also included is an excel solution. The screenshot of the excel solution is shown below.

Cut the data table by the max date and add your calculatins. The maxDate-1 you will get from Matrix visual for each row in matrix. Then add caclulated values to each row. Then filnal culculation. The idea can loks like this (I did't check):
VAR Y36Value=
CALCULATE(
SELECTEDVALUE(table[Y])
,LASTDATE(table[DATE])
)
VAR filteredTable =
FILTER(
All(table)
, table[starting inv]<MAX(table[starting inv])-1 && Y36Value <> 0
)
VAR fltdTblWithValues =
ADDCLUMNS(
filteredTable
,"SumProductEF", [E]*[F]
,"SumProductYZ", [Y]*[Z]
)
VAR result =
DIVIDE(
SUMX(fltdTblWithValues,([SumProductEF]-[SumProductYZ])
,SUMX(fltdTblWithValues,E-Y)
)
If you add your values in a table format then I can check the measure.

Related

HOW TO REFERENCE A MEASURE IN A FILTER (DAX)

I have created a measure
Report_Day = MAX(DATE_LT[Date])-2
I want to create measures where they reference this measure so i can just change -2 to -1 and the rest of measures work depending on the reporting date.
when i use this measure:
Day_outlet_Txns = CALCULATE(
[outlet_Dep_Txns],
FILTER(ALL(DATE_LT),[Report_Day])
)
i get a value equal to tatal transactions in a table yet i want for transactions of that report day.
When i try something else
Day_outlet_Txns = CALCULATE(
[Issuer_Dep_Txns],
FILTER(ALL(DATE_LT), DATE_LT=[Report_Day])
)
i get an error: The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
enter image description here

Need to filter result set in DAX Power BI

I have following simple relationship:
I have created following visuals in Power BI:
I want to show Store Name, Orders (by Salesman selected in slicer) and Total Orders in that Store (ignoring Salesman selected in slicer). I have created two very simple measure (can be seen in above visual) and used in matrix visuals. Visual is showing All stores while I want to show only those stores where Salesman X (selected salesman in slicer) have orders i.e. I don't want Store B row.
while solving, I suspected that it is due to fact that visual is not cross filtering. I used crossfilter but it made no difference. data can be seen in below image:
Please guide. Thanks in advance.
Try to change [Total Orders] to this measure, but keep [Total Orders].
IF( ISBLANK([Orders Count]), BLANK(), [Total Orders])
By Adding VALUES('Order'[Store ID]) in measure solved the problem. complete measure definition is as follows:
Total Orders = CALCULATE(
count('Order'[Order ID]),
REMOVEFILTERS(Salesman[Salesman Name]),
VALUES('Order'[Store ID]))
This issues the problem but I could not understand how? Because VALUES bring only those stores where salesman has Order. But when salesman removed from the filter context by REMOVEFILTERS, then how come VALUES bring only stores where salesman have orders?
a) You intend to utilize Store.salesmanName from Store in a slicer, meaning whatever is selected from there, you intend that selection to be applied on Order to give you the Order.StoreName. So when X is selected only A and C are returned.
b) Once that selection happens, you intend DAX to return the total count of each Order.StoreName whether it has a corresponding Store.salesmanID in Order.salesmanID or not. In other words, in this layer of the analysis, you want the previous selection to remain applied in the outer loop but to be ignored in the inner loop.
To be able to do that, you can do this,
totalCount =
VAR _store =
MAX ( 'Order'[storeID] ) //what is the max store ID
VAR _count =
CALCULATE (
COUNT ( 'Order'[SalesmanId] ),
FILTER ( ALL ( 'Order' ), 'Order'[storeID] = _store ) //remove any filters and apply the value from above explicitly in the filter
)
RETURN
_count

Reuse calculated table variable within DAX formula

I have the following DAX formula for a measure, and it works fine.
Gross Area =
var as_on = LASTDATE('Calendar'[Date])
var all_latest_dates =
ADDCOLUMNS(
VALUES('Unit Revision'[Unit Id]),
"Last Event", CALCULATE(
MAX('Unit Revision'[Event Date]),
'Unit Revision'[Event Date] <= as_on
)
)
var unit_latest_dates = TREATAS(all_latest_dates, 'Unit Revision'[Unit Id], 'Unit Revision'[Event Date])
RETURN
CALCULATE( SUM('Unit Revision'[Gross Area]), unit_latest_dates )
I need to calculate more metrics using a similar logic, where the DAX formula is the same until the RETURN statement, and just the column name within the final CALCULATE( SUM() ) differs.
I then need to add all these measures to a Matrix. In such as case, will the variable calculation of theĀ unit_latest_dates table be calculated for every DAX measure, or will it be cached? To ensure it is cached, is it possible to reuse the initial set of DAX code within a calculated table measure or something, just like how we can reuse a scalar measure in another measure?
I can't use this in a Calculated Table, as the calculation changes based on changes in the Calendar Table Date slicer.
Yes, it should be cached as long as DAX Engine can understand that a similar query was executed earlier and it didn't involve a CALLBACKDATAID and was executed only by Storage Engine and not Formula Engine, try to use DAX Studio and see if it is doing that or not.

DAX formula to subtract columns

I am trying to calculate month over month difference but it makes data negative.
I created a measure, but it makes source data negative.
CALCULATE (
COUNTA ( SOURCE_DATA[COLUMN] ),
FILTER ( SOURCE_DATA, SOURCE_DATA[YYYYMM] = "201906" )
)
- (
CALCULATE (
COUNTA ( SOURCE_DATA[COLUMN] ),
FILTER ( SOURCE_DATA, SOURCE_DATA[YYYYMM] = "201905" )
)
)
The outcome is correct, but it changes data in previous month to negative.
This is due to the filter context and the way you've written the measure.
Look at the visual table. For the field corresponding to Column = 201905 and row = GA you get -16 813. This is because the context of the visual table tells CALCULATE to COUNTA(SOURCE_DATA[Column]) only when MtM = GA and Columns = 201905. However, adding the FILTER you also tell CALCULATE to keep these criteria AND also make sure that SOURCE_DATA[Column] = 201906 in the first calculate and 201905 in the second one.
This results in CALCULATE looking for rows where Column is both 201905 and 201906 at the same time. Or in other words you generate a venn diagram with no overlapping fields. Therefore the first calculate evaluates to 0 and the second to 16 813, so that the measure is actually evaluating 0-16813 = -16 813.
Since you didn't post any description of your data model I can inly guess what it looks like. However, since you're filtering on the SOURCE_DATA table I guess you don't use a Calendar table. This you should do! Have a calendar with a 1:* (1-to-many) relationship with the SOURCE_DATA and do filtering on the calendar. In addition you can have dynamically calculated day/week/month/year offsets so that you can create measures which don't have to be updated when there's a new month.
I think this video can be helpful: sqlbi videolecture
Also, have a look at this article: sqlbi filter in calculate

How to find the maximum value from a column satisfying two or more IF conditions in DAX

I am a newbie to Power BI and DAX.
I have a dataset as attached. I need to find the maximum value for each person for each week. I have written the formula in Excel.
=MAX(IF(A$2:A$32=A2,IF(D$2:D$32=D2,IF(B$2:B$32=1,C$2:C$32))))
How can I convert it to DAX or write the same formula in Power BI? I tried the DAX Code as below, But it did not work(ALLEXCEPT Function expects table).
Weekly Maximum =
CALCULATE ( MAX ( PT[Value] ), ALLEXCEPT ( PT, PT[person], PT[Week],
PT[category] ==1 ) )
Once I calculate this, then I need to calculate the Expected value for each week, that has the maximum value of the previous week * 2.85, as shown in the screenshot. How can I put the previous week's maximum value for this week?
Any corrections/solutions, please?
TIA
The Max Value for Category 1 can be written like this:
= CALCULATE(MAX(PT[Value]),
ALLEXCEPT(PT, PT[Person], PT[Week]),
PT[Category] = 1)
(The Category filter doesn't go inside ALLEXCEPT().)
For your Expected Value column, you can do something similar:
= CALCULATE(2.85 * MAX(PT[Value]),
ALLEXCEPT(PT, PT[Person]),
PT[Category] = 1,
PT[Week] = EARLIER(PT[Week]) - 1)
(The EARLIER function gives you the value for the row you are in. The name refers to the earlier row context.)

Resources