DAX - Nested Calculated queries - nested

I have a large data file (4.2mil rows and 10 columns) and I'm trying to do some filtering with measures. However, when I try to use nested measures everything explodes and I'm not really sure why.
So far, I have two explicit measures:
MaxValue:=MAX('Element Forces - Area Shells'[Value])
and
MaxIndex:=
CALCULATE(
FIRSTNONBLANK('Element Forces - Area Shells'[Index],1),
FILTER(
ALL('Element Forces - Area Shells' ),
'Element Forces - Area Shells'[Value]=MAX('Element Forces - Area Shells'[Value])
)
)
This works and I get a correct values. However, as I see it, I should be able to put the MaxValue measure into the MaxIndex measure, but then everything just stops.
The new measure I'm picturing should look like this:
MaxIndex:=
CALCULATE(
FIRSTNONBLANK('Element Forces - Area Shells'[Index],1),
FILTER(
ALL('Element Forces - Area Shells' ),
'Element Forces - Area Shells'[Value]=[MaxValue])
)
)
But it just doesn't work. What am I doing wrong?

Although I can't test it right now, I think it's caused by the implicit context transition*. When you're referencing a measure in another measure, internally a CALCULATE() is involved and therefor a context transition. That's not wat you want in this case. I advise you to use a variable, in order to create one measure:
MaxIndex:=
VAR MaxValue = MAX('Element Forces - Area Shells'[Value])
RETURN
CALCULATE(
FIRSTNONBLANK('Element Forces - Area Shells'[Index],1),
FILTER(
ALL('Element Forces - Area Shells' ),
'Element Forces - Area Shells'[Value] = MaxValue )
)
)
Note: I'm not sure, please forgive me if I'm wrong :)
In addition, the use of VAR, from the MSDN docs:
This feature is included in SQL Server 2016 Analysis Services (SSAS), Power Pivot in Excel 2016, and Power BI Desktop only. Information provided here is subject to change.

Related

How to Generate a Burndown by Month

I have a dataset of action tracking records with due dates. I've been asked to generate a burndown curve, which I can do pretty simply in Excel, but I'm stumped on how to put this in a WebI report.
I created a variable to convert the due date for the records to yyyy-MM, so I can group them by the month they're due and count them. Then, I was able to add a runningsum of those counts in my crosstab.
[
The element that I'm missing is being able to refer to the total count of records (the total outside the column grouping of the crosstab) to subtract the running sum from.
Suggestions?
You need to deal with Calculation Contexts in order to do this. Here are a few good resources...
Removing the Confusion from Calculation Contexts
Calculation Context Part I: Overview
The More Things Change... (de facto Part II to Part I above)
To answer your question and show my work I created a free-hand SQL query using the SQL in this dbfiddle. Put into a crosstab with a running sum I get what you have in your example...
Next I created a variable called Var Report Total defined as...
=Sum([Act Count]) In Report
Then I created another variable called Var Remaining to calculate the difference...
=[Var Report Total] - [Var Running Sum]
And there you have it...

How to subtract two time difference rows in the same column (like A2-A1) but in PowerBI

Hello PowerBI (DAX) lovers,
I cant find suitable DAX function which will help me to calculate (subtract) two different time periods, so A2-A1 would be Excel like and so on. In Excel it is easier with MOD function, I am familiar with that, and works good. But, thing is, I would like to do all the stuff of my analytics in PowerBI, not partly Excel (like this) and then I have to upload xlsx. files and work with them.
I was looking for some examples but none of them was good enough to calculate this. The only thing I did now is inserting Index column which might be starting point..but I am looking for your experience?
This is the data set that I isolate part per part, and the column that I would like to solve is Time column , where I wanna subtract second number against first (17:00 - 13:00 and so on..) accordingly.
Thank you in advance!
Create a calculated column that combines Date and Time so you can subtract across days. Let's call it DateTime.
Then your calculated column for the difference will look something like
TimeDiff =
ExpLog[DateTime]
- MAXX (
FILTER ( ExpLog, ExpLog[Index] < EARLIER ( [ExpLog[Index] ) ),
ExpLog[DateTime]
)
Instead of MAXX, you could probably use a lookup instead
TimeDiff =
ExpLog[DateTime] - LOOKUPVALUE ( ExpLog[DateTime], ExpLog[Index], ExpLog[Index] - 1 )
So with the suggest and help of user #Alexis Olson I managed to extend and to solve my small problem: This was DAX that I used in calculated column:
TimeDiff = 'Explog2019-08-07'[Time]
- LOOKUPVALUE('Explog2019-08-07'[Time];'Explog2019-08-07'[Index];
'Explog2019-08-07'[Index]+1)
So it was possible to solve it without connecting date+time as previously suggested.

Sigma Level calculation in Excel

I need to calculate the number of levels to then apply a formula in Excel. It's for database pages in calculating the amount of space needed for indexed tables. The formula, as it's presented by Microsoft, can be found here:
SQL Server Estimate Size of NonClustered Index.
The formula, in transcribed mode is:
Num_Index_Pages = Sigma Level(Num_Leaf_Pages/Index_Rows_Per_Page^Level)
I need to apply this formula in Excel to plug in many different values to find the total space needed for a database (all the tables, with indexes, adds up to the total of the database projected space requirements).
What I've done so far has not worked, and I admit that I'm not very familiar with Excel functions beyond sum...
The starting values, which will change for each table/index are as follows:
Num_leaf_pages=27778
Index_Rows_Per_Page=42
Non-Leaf_Levels=5
So, plugging in the values, I need a formula that will do the following:
27778/(42^5) + 27778/(42^4) + 27778/(42^3) + 27778/(42^2) + 27778/(42^1)
When I try to do this in Excel, I cannot get the correct value. I've looked online and have found "SUMPRODUCT" that uses "ROW" and "INDEX" and something called "INDIRECT", and I don't understand how any of these work.
Thanks in advance for any help you can provide.
As you stated, Use SUMPRODUCT with ROW(INDIRECT()) to do the iteration:
=SUMPRODUCT(B1/B2^ROW(INDIRECT("1:"&B3)))

Using Multiple filters in DAX

I'm new to DAX.
I'm currently using Power BI and trying to create a total sum of sales that use a few different SKUs (or IDs)
I can use this to filter down to 1 sku ("A1"):
Measure = CALCULATE([Sum of Sales],Table4[SKU]="A1")
but I would like to filter down to five different SKUs.
Example Data:
2 1,050.54
3 43,925.20
4 8,596.00
5 1,630.00
8 3,330.00
A1 45.24
A2 499.87
A3 53,567.05
A4 337.92
A5 4,265.00
AB 12,651.94
ACC 7,634.95
ADV -1,769.95
ANT 1.60
AUTO 9,655.40
BOOT 268.00
Is this possible?
CALCULATE is defined as CALCULATE(<expression>,<filter1>,<filter2>…) This means that you can use multiple filters at one time. However, the multiple filters will act at the same time. Meaning that the data would have to meet both conditions.
So doing BadSumOfSales:=CALCULATE([Sum of Sales],Table3[SKU]="A1",Table4[SKU]="AB") will not give you what you need. Since the SKU would have to be equal to A1 and AB, it will return blank
Since you have five items you would like to included in your filtering, you should leverage the SWITCH function. This will allow you to accommodate multiple conditions that should return TRUE and then let you return FALSE for anything else
TotalsSumOfSales:=CALCULATE([Sum Of Sales],
SWITCH(Table4[SKU],
"A1",TRUE(),
"A2",TRUE(),
"A3",TRUE(),
"4" ,TRUE(),
"5" ,TRUE(),
"8" ,TRUE(),
FALSE()
))
Another way to get around this would be using the OR function. This is a great option, but only really works well when you have two filters at a time. If you have more than two, you will have to do some nesting that can get complex. So for your case I would stick with the SWITCH, but here is an example of how it would look:
OrTotalSumOfSales:=CALCULATE([Sum of Sales],
OR(
Table4[SKU]="A1",
Table4[SKU]="A2"
))
The best idea in this case is to use IN Operator in DAX. The Performance is way better than pipe operator or OR and the code becomes more readable.
This is only supported in the latest versions of DAX.
GroupingSales:=CALCULATE([Sum of Sales],Table[SKU] IN {"A1","A2","A3","AB"})
You can also use CONTAINSROW.
More info: https://www.sqlbi.com/articles/the-in-operator-in-dax/
Just a third way alternative to the Or version.
You can use the double pipes '||' which acts as an Or statement in the filter.
Measure = CALCULATE([Sum of Sales],Table4[SKU]="A1" || Table4[SKU]="A2" ||
Table4[SKU]="A3" ||Table4[SKU]="A4" || Table4[SKU]="A5")
Well I think based on your request to filter down to 5 different SKUs that you actually shouldn't be using DAX to solve your problem.
If you just insert a pivot table in Excel you can add SKU to rows and sales to values. If you need, you can adjust the aggregate (but I think it defaults to SUM) so you should already have the right data. Only thing remaining is to filter it down to just the SKUs you want. You can do the same in Power View just by creating a table. Then you can add SKU to the filter pane and select the 5 skus you want.
Appreciate your using Power BI,
-Lukasz
http://dev.powerbi.com
http://blogs.msdn.com/powerbidev
Make a feature request: https://support.powerbi.com/forums/265200-power-bi
Sign up for Power BI: http://www.powerbi.com

Which is the most flexible way for end-user to create a report querying an SSAS Cube?

I know that you can query a cube by connecting from Excel to Analysis database and using formulas like cubevalue() or cubemember().
I also know that after converting the power pivot to formulas, you can access the attribute and the value related only by writing a text.
Example: for Branch Dimension, instead of writing
cubemember("connections";"[DimBranch].[Name].[All].[London])" )
you can write in the cell only "London". However, this won't work if you have a parent-child dimension and want to retrieve the amount for one of the intermediate levels.
Did anyone know about how can you avoid writing these formulas directly by the end-user ?
I´ve done this several times. I can describe the approach I´ve used in the past.
You need to define the input from the user, for example Fiscal Year, Fiscal Period,... and create a tab with a friendly layout for this solely purpose.
After that you create your pivot/pivots against the cube/s you want to query with the data extraction that you need (obiously these pivots will refer to the Fiscal Year and Period). Define the desired layout for these pivots (as you want them you be shown). Once You´ve done this, you need to convert into formulas these pivots, including parameters. You´ll see that values placed in the measure box contain a formula cubevalue which makes reference to the connection itself and several cubemembers. It makes reference to the measure as well.
The trickiest part comes now, when you need to modify your cubemember function to take value from the input, something like cubemember("connections";"[DimBranch].[Name].[All].["+MyReferenceCell+")" ), and you have your dimension picked up from a value entered by a user.
If you have as a matter of fact, serveral pivots referencing common dimensions, you need to make the reference this modified cubemember cell affected by user input, otherwise you have to modify all affected cubemembers in your pivots converted to formulas.
This has a really good advantage: depending on the excel´s version, all pivots are automatically recalulated if you change the value in the user´s input cell without hitting the refresh all button.
On the other hand, the disadvantage comes when your report in excel contains a list of values from a dimension which can grow. After converting pivots into formulas those new values for the dimension are lost. That´s why you can always place a control if you have the total value int he pivots regardless the dimension value.
I hope this helps.
Regards

Resources