How to calculate a hierarchy level in Dax? - powerbi

I need some help with a dax measure. Below you see a matrix in Power BI Desktop. What I want is to calculate the ratio of the Sum(of 0 and 1 level) divided by the measure [Total Salg vekt], which I already has calculated. But I am struggling getting the sum of the second level of my product hierarchy. I don't want to calculate level 1 or 3 in the hierarchy.
My hierarchy is located in my Product table and is named like this
Products
- Category
- Code
- SubCategory
Can any of you show a dax code that my work as described above?
Thanks
Regards Geir

I have bad news:
There's no hierarchy in DAX.
You'll have to use different filters in order to achieve what you want.
Complete explanation is in here:
https://www.sqlbi.com/articles/clever-hierarchy-handling-in-dax/
And yes, it is not simple, but it answer your question.

Related

Top N and Others in Power BI

I have a simple database with a doctors table and a detail table with their patients.
How can I efficiently group doctors with less than 300 patients into an other doctors category to obtain a pie chart like the picture below?
What I end up doing is to create a DAX table
DoctorsWithPatientsCount = SUMMARIZE(Patient, Patient[Doctor.FullName], "PatientCount", COUNT(Patient[Id]))
with the following calculated column
TopDoctor = SWITCH(TRUE(), 'DoctorsWithPatientsCount '[Count] > 300, DoctorsWithPatientsCount[Doctor.FullName], "Other doctors combined")
and have that ploted.
As you can see it does work, however, is there another more direct and efficient way to achieve this? Thanks!
TopN and Others has been a long requested feature in PBI which you can vote on here: https://ideas.powerbi.com/ideas/idea/?ideaid=08369fa0-5152-4d55-b858-e36c06737d10
There are currently a few ways of achieving this.
Your way which has the disadvantage of not being dynamic and responding to slicers because it is a calculated table. Usually, you would use a RANKX or TOPN rather than your DAX.
If you want the TopN to be dynamic, you need a disconnected table. There are simple implementations described here: https://goodly.co.in/top-n-and-others-power-bi/ or more complicated but versatile approaches described here: https://www.sqlbi.com/articles/filtering-the-top-products-alongside-the-other-products-in-power-bi/
You can use a custom visual like Deneb to perform a window transform that calculates the TopN for you and displays it automatically.
As an addendum, your pie chart should really only display 5 or 6 categories max to be useful to end users.
In doctors table, you can define a new computed column "computed doctor"; then use this new computed column to make pie chart.

How to add same measure used in different vizualiztions. power Bi?

I have a measure [expense]. I have two filters filter1(current year expense) and filter2(prev year expense)
I have created two table in a Power Bi report, and used the same measure expense in them both of them such that,
The first table ignores filter 2(prev year expense) and only filter1(current year expense) is applied
The second table ignores filter1 and only filter 2 accepts.
Basically I want to have Current year and prev year expense in two separate tables. This I am able to achieve quite easily, using edit interactions.
But the problem is I also need a third table which should give the result as the total of the two tables. Since I am using the same measure in both the tables, how can I achieve this.
Note:: My total table total summation should change based on filters selected .
Basically table3=table1+table2.
Try something like this. Filter the third table with filter1(current year expense). I do not see better solution within your report logic.
CALCULATE(
[expense]
,tbl[Year] In {SELECTEDVALUE(tbl[Year]),SELECTEDVALUE(tbl[Year])-1}
)

power bi measure with 2 conditions 2 calculate

I am trying to create a measure which will calculate the Total Project Revenue
while I have 2 different projects. For each project there is a different calculation:
for Hourly project the calculation should be: Income * BillHours
for Retainer project the calculation should be: Income*TotalWorkinghours
I wrote the below DAX:
enter code here : Total project revenue = IF(max(DimProjects[ProjectType])="Hours",
max(FactWorkingHours[Income])[BillHours],max(FactWorkingHours[Income])*
[Total Working Hours])
the rows are calculated correctly but the total in the table is wrong
what should I fix in DAX so the total of all raw will correct as well.
The total Revenue should be 126,403.33
Thank you in advance
you can find here the table with the results
It's hard to say exactly what your measure is doing because, as written here, that is not a valid measure. I pasted it into the DAX Formatter, which I would recommend for formatting and pasting here into code blocks, and the measure was invalid. It would also be helpful to post the other measures this measure references, eg. [Bill Hours] and [Income Hours].
That being said, I think I can kind of tell what's going on. Your total is probably wrong because the filter context at the total level is being based of the condition where:
MAX ( DimProjects[ProjectType] ) = "Retainer" (or some other value not in your shared snippet)
That is because when you consider the MAX of a string, the higher alphabetical order is considered. Therefore, "Retainer" > "Hours". So at the total level, your table is outputting—more than likely, I can't be certain without more information—the false condition of your measure:
MAX ( FactWorkingHours[Income] ) * [Total Working Hours])
There is a better way to handle your intended outcome. IF statements, in the way you are attempting to use it, are rarely used in a calculated measure. You may be better off trying a calculated column without using the MAX functions. Again, I can't give an exact code recommendation without more context. Hope that sends you in the right direction!

Is DAX from Power BI same as LOD in Tableau

I'm new to tableau. my question is, is DAX from Power BI same as LOD in Tableau?
It'd be great if someone could help me with an explanation for this.
thanks in advance!
There are similarities for sure. In Excel it can be a way to display a value that is unaffected by filtering.
LODs are similar in that you can fix your value.
For example, if I want to know the total sales in a workbook regardless of what row/column the value is on, I could use a FIXED LOD expression:
{FIXED : SUM([Sales]}
If you put the above on Label and a Month dimension on rows, it will ignore the segmentation of months and display the total sum of sales for the entire workbook.
You can choose to fix on a different level of detail to achieve a different result:
{FIXED [Month] : SUM([Sales]}
If you put the above on Label and a month and date dimension on rows it will display the total sum of sales for each month, and ignore the date. And so on...
They're somewhat tricky to get used to, but they come up a lot in more complex workbooks.
I would put it this way, the DAX language is at least as powerful as Tableau expressions (involving LOD or otherwise). That is, if you can do it with Tableau LOD, then there's also a way to write something equivalent with DAX.

Filtered LOD calculations in DAX expression Power BI

I have created LOD expressions in Tableau before which dynamically calculate when filters are applied. I am trying to achieve similar functionality in Power BI. I am very new to Power BI so I probably didn't ask google the right question. I simply want a column that shows the average sales over my "Filtered" data set. The closest I cam was using the Calculate(average(table[column]),All(table)) but that does not dynamically change when I apply the date slider. If I filter for 1/1 - 1/5 I want that average. Change the date range, the average should change.
Thank you.
You are probably looking for the ALLEXCEPT() function. In this case you would do something like
CALCULATE(AVERAGE(table[column]), ALLEXCEPT(table, table[date]))
that is basically saying that you want to remove all filters on the table except the filters on the date column.
CALCULATE(
AVERAGE(Table[Column]),
ALLSELECTED()
)