How do I get my DAX measure to calculate grouped values? - powerbi

I need help with a DAX measure in my Power BI report. I am just learning about measures, so sorry if this seems like a newbie question.
Here’s my scenario:
The purpose of the report is to show water usage for various locations in a municipality. The data is coming from IOT sensors, with new entries every few minutes or so.
A sample of the data generally looks like this:
|SensorID |Timestamp |Reading
|----------|---------------------|--------
|1 |2017-06-22 12:01 AM |123.45
|1 |2017-06-22 12:15 AM |124.56
|1 |2017-06-22 12:36 AM |128.38
|2 |2017-06-22 02:12 AM |564.75
|2 |2017-06-22 02:43 AM |581.97
I have a simple DAX measure that I use to calculate water usage for each location/sensor, for the current date range selected in the report (via Timeline control):
Usage:= (MAX([Reading]) - MIN([Reading]))
This measure works great when a single location/sensor is selected. However, when I select multiple locations, the calculation is incorrect. It takes the MAX value from ALL sensors, and subtracts the MIN value from ALL sensors - rather than calculating the usage from each location and then summing the usage.
For example, given the data sample above, the correct calculation should be:
Total Usage = (128.38 - 123.45) + (581.97 - 564.75) = 22.15
Instead, it is calculating it this way:
Total Usage = (581.97 - 123.45) = 458.52
How can I get the measure to calculate the usage, grouped by the Sensor ID?
I hope this makes sense.
Thanks!

Try this:
Total Usage:= SUMX( VALUES(MyTable[SensorID]), [Usage])
VALUES(MyTable[SensorID]) function gives a list of unique SensorIDs. SUMX function then goes over that list one by one and calculates your [Usage] measure per SensorID. Then it sums up the results.
An alternative solution:
Total Usage:= SUMX( SUMMARIZE(MyTable, MyTable[SensorID]), [Usage])
It works the same way, only the list of unique sensor ids is returned by SUMMARIZE function instead of VALUES.
Results:

Related

Why I get different Total, but same values?

I needed to replace this measure:
CALCULATE([GM % YTD], SAMEPERIODLASTYEAR('Date'[Date]))
By this one:
VAR VAR1 = ADDCOLUMNS( VALUES(Revenue[Key_Client]),
"Col1", CALCULATE([GM % YTD], SAMEPERIODLASTYEAR('Date'[Date]),
REMOVEFILTERS(Revenue[Type],Revenue[SectorType]))
)
RETURN AVERAGEX(VAR1, [Col1])
Both measures point to GM % YTD, which is:
CALCULATE([GM %], DATESYTD('Date'[Date],"31/05"))
I get this, when I display them side by side:
The values are ok, my problem is with the Total. I am unable to find how/where is the aggregation on the left column done... How is that 73,2% achieved? It doesn't seem to be average...
Also… how can I force the measure on the right to do the same aggregation?
In the ADDCOLUMNS version, you are iterating over each Revenue[Key_Client] and only averaging after the [GM % YTD] has been calculated for each one separately. For a single client, there's only one thing to average, so the value isn't affected by that step.
Generally, you want to compute the measure over all clients together rather than averaging the individual numbers together to get a standard weighted average rather than an average where all clients are weighted equally.

Toggle between different "Show Values As" options in Slicer

I am using a Matrix in PowerBI to show the Sales Manager and Sales Reps down the rows, and the different ship method and source of the order across the columns and a count of unique orders in the values.
Below shows the table structure I have, and I want to switch between "Show Values As": the original values I calculate with my measure (count of unique orders) and % of Row Total.
Currently, my only solution is to create a second table showing the different values, but I think that's a cheap solution. I've seen solutions with helper tables but since my values are a measure I cannot invoke them in other measures, and as the sources or ship methods are filtered I want the % of row total to reflect the filter as well so using a countx formula won't account for that (if I housed it in a formula).
I'm still somewhat new to the software so please bear that in mind.
Source 1 | Source 2 | Source 3
| Next Day; Ground; 2 Day | Next Day; Ground; 2 Day | Next Day; Ground; 2 Day
+Manager 1|
Rep 1 |
Rep 2 |
+Manager 2|
Rep 1 |
Rep 2 |
Looks like the best solution for you is to create bookmarks. You can create two tables, one showing the values and the other showing the %. Position the tables such that, one table is exactly on top of the other. Then create two bookmarks, one showing Table1 and the other showing Table2 (while hiding the other table using the "Selection Pane"). You can then use the bookmarks in a button action to toggle between Value and % views. The following link should give you further details on bookmarks:
https://learn.microsoft.com/en-us/power-bi/desktop-bookmarks
You can also do this by creating a measure which switches between values and %, but the formatting is going to be a pain point. I think bookmarks is your best bet.
You can create a parameter table (with no relationships to other tables in your model) to use as a slicer and a measure that switches between the two calculations based on a slicer selection.
Let's suppose you have measures [OrderCount] and [OrderCount%Total]. Then create a new table to use as a parameter ParamMeasure with a single column
Measure
-------
Count
%Total
You can now put ParamMeasure[Measure] as a slicer on your report and substitute the following measure instead of the existing ones.
OrderSwitch =
IF (
SELECTEDVALUE ( ParamMeasure[Measure] ) = "Count",
[OrderCount],
[Order%Total]
)
This way you don't need to create multiple visuals and bookmarks.

Rolling Average - Calendar Table and slicers

I'm having an issue that i can't seem to solve on my own:
I have a model that has two tables.
Fruit_data_Table
Date
Fruit_id
Fruit_category
Fruit_scheduled_picking_time
Fruit_real_picking_time
Fruit_picked_within_1min = 1 or 0
Fruit_picked_within_5min = 1 or 0
etc etc
On every given day i have 700 different fruit entries on my fruit_data_table.
I created a Calendar_Table using a classic :
Calendar_Table= CALENDAR(MIN(Fruit_data_Table[Date]);MAX(Fruit_data_Table[Date]))
And then i needed combined averaged values for any given day, including rolling averages, so inside my Calendar_Table i added some calculated columns for every dates such as :
Fruit_on_time_1_min_pick=
VAR this_date = Calendar_Table[Date]
RETURN CALCULATE(
SUM(Fruit_data_Table[Fruit_picked_within_1min ])/COUNT(Fruit_data_Table[Fruit_picked_within_1min ]);Fruit_data_Table[Date] = this_date)
Or a rolling average :
Fruit_on_time_1_min_pick_7day_average=
VAR this_date = Calendar_Table[Date]
RETURN CALCULATE(
SUM(Fruit_data_Table[Fruit_on_time_1_min_pick])/COUNT(Fruit_data_Table[Fruit_on_time_1_min_pick]);
DATESBETWEEN(Calendar_Table[Date];DATEADD(LASTDATE(Calendar_Table[Date]);-6;DAY);LASTDATE(Calendar_Table[Date])))
Which mean i ahve something like
Calendar_Table
Date
Fruit_on_time_1_min_pick (%)
Fruit_on_time_1_min_pick_7day_average (%)
etc etc
This all seems to work pretty well and i have my daily rate of fruit pciked on time and rolling averages.
The problem is that then i can't use my slicer to sort by fruit_category, or by time_of_day for example... any idea how i can get back to those slicers ?
(The PowerBi rolling average function don't seem to work at all for me, even though my Calendar_Table is a PowerBi generated Date Table...)
Thank you very much !
PS: real code is about train numbers is a major french train station... not really about fruits :)

Sharepoint calculated column date calculation

I have a list in SharePoint Office 365 with a number of columns to register and track claims.
One of the columns is a calculated one - to catch the duration of claims processing.
I would like to calculate it as a difference between dates and shown in days.
To do it I want to use 3 pieces of information:
Column [RegistrationDate]
Date of new registered claim.
Column [EndDate]
Finalization of the claim.
[Today]
Sytem date for now.
The logic to be done by the formula should be:
IF [EndDate] is empty THEN:
[Today]-[RegistrationDate]
OTHERWISE
[EndDate]-[RegistrationDate]
Question: What formula will achieve my desired output?
Calculated columns cannot use the [Today] dynamic variable. (Validation formulas can.) Calculated Columns are only updated when they are edited, not each time they are viewed.
If you could use [Today], it would look like this:
=IF( ISBLANK( [EndDate] ), [Today]-[RegistrationDate], [EndDate]-[RegistrationDate] )
But... you cannot use [Today] in a calculated column...

DAX: How to correctly create a measure group from a range of dates?

I have a dataset more or less like this one:
DATE | VALUE
01/01/2011 | 100
02/01/2011 | 150
02/01/2011 | 550 --> Repeted on purpouse
.
.
12/01/2016 | 320
Now I need to have a calculated measure with only the values within a date range, I tried in many ways but with no success, the only one I managed to get it work is the follow DAX syntax:
consuntivo = CALCULATE(SUM(provadat[valori]);provadat[datazione]>=DATE(2015;01;01)&&provadat[datazione]<=DATE(2016;01;01))
but it generates the following:
So basically I need a DAX Query with distinct sum for each dates. How can I achieve that?
Two methods.
In the Table visualization you can choose Sum as the summarize option for the column valori.
Or using DAX, it'll be just simple as
consuntivo = SUM(provadat[valori])
You don't need to handle the date logic particularly because Power BI will handle it based on the context (data columns you used with the measure).
So basically what I was missing was to add filters.
xxx = Calculate(SUM(provadat[valori]);FILTER(VALUES(provadat);provadat[datazione] <= DATE(2017;01;01) && provadat[datazione] >= DATE(2016;01;01)))