Table 1:
Table 2:
I need a measure that will do SUM( every Value2* corresponding Value1)
I have tried the following but it does not work - I assume due to the tables being different number of rows.
Measure = SUMX(Table2, Table2[Value2] * Table1[Value1])
Assuming you have a relationship like this
use this measure
Measure =
SUMX(
Table2,
Table2[Value2] * RELATED(Table1[Value1])
)
to get this result:
Related
I have a simple table here:
I need to make a measure that counts records grouped by the Number column.
Ideally:
I also need this measure to by dynamic for whatever records are selected. So if a subset of the table is being used:
(Same example table, but with 2 less records)
Then the ideal output would be:
Now output for number '1' is 2 instead of 4.
Some of the code I tried:
,"CNT",COUNTROWS(ALLSELECTED([Table1]))
,"Summz",SUMMARIZE(ALLSELECTED(Table1),"CNT",COUNTROWS(Table1))
,"CALC",CALCULATE(COUNT(Table1[Rownum]),ALLEXCEPT(Table1,Table1[Number]))
,"CALCII",CALCULATE(
SUMX(
ADDCOLUMNS(
SUMMARIZE(Table1,Table1[Rownum])
,"Freq",DISTINCTCOUNT(Table1[Number])
),
[Freq])
,REMOVEFILTERS(Table1[Rownum])
)
I feel like I'm close but not getting it. Any help would be appreciated!
Thank you
Try this.
Freq = CALCULATE( COUNT(Table1[Number]), ALLEXCEPT(Table1,Table1[Number]))
Step1:
I have created a calculated table containing Level, Location, L2code from level sales data. I need to create a report that will count rows based on the level1 group.
Note that there are more levels in the table. This is only an example of one of the levels.
How can I count the rows for each level1?
Step2:
I need to create all combinations of counts based on location and l2 code and count the numbers.
like in example 2 location and 8 distinct l2code so it should be 2*8 =16 total possible rows.
How can I achieve this using the DAX measure?
Source data file
Output report
first one is a simple measure as below-
DistinctRowCount = Count(your_table_name[Level1])
Second one is bit tricky and you can try this below measure-
CrossCount =
var distinct_location =
calculate(
distinctcount(your_table_name[Location]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location]
)
)
var distinct_l2code =
calculate(
distinctcount(your_table_name[l2code]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location],
your_table_name[l2code]
)
)
return distinct_location * distinct_l2code
Sample output-
I have a slicer, called COUNTRY and applied to table MY_TABLE. When I calculate a measure, everything works as expected:
-- calculates distinct count only for COUNTRY = x
Some Measure = DISTINCTCOUNT('MY_TABLE'[SOME_COLUMN])
The problem is SUMMARIZE ignores slicer selection:
-- calculates distinct count accross all countries: x, y, z, etc.
Calculated Table =
RETURN SUMMARIZE(
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT('SOME_TABLE'[SOME_COLUMN])
)
How to make SUMMARIZE take into account slicers?
Only Measures are "responsive", calculated tables and columns get calculated and created once, when the data are loaded.
Note that if a calculated table is used inside a measure it will behave correctly, but as you may know, a measure must return a scalar value and not a table. (ie you can use summarize inside a measure, you can then filter the obtained table and return the sum of one column)
Of course, you can filter calculated table with a slicer. If you can, go for SUMMARIZECOLUMNS because this function is better optimized then SUMMARIZE, and has arguments for filtering.
Filtering SUMMARIZECOLUMNS
If you want to stick to SUMMARIZE, you can filter your table by wrapping it with CALCULATETABLE.
Calculated Table =
CALCULATETABLE (
SUMMARIZE (
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT ( 'SOME_TABLE'[SOME_COLUMN] )
),
Dim[Color]
= SELECTEDVALUE ( Slicer[SlicerValues] )
)
Should FILTER be used inside or outside of SUMMARIZE?
I have two tables:
A calendar table with both dates and hours.
And a table that contains incidents, with start time and end time in addition to set of attributes (only one, incident_code, in the example table).
For a specific date range, I want to show how many incidents occurred each hour.
The following measure works, but struggles when the incident table becomes large and there is several slicers
incidentCnt =
CALCULATE(
COUNTROWS(incidents);
FILTER(
incidents;
incidents[start_datetime] < MAX(date_hour[datetime]) &&
incidents[end_datetime] > MAX(date_hour[datetime])
)
)
What would be a more efficient way to calculate this with DAX in Power BI?
EDIT: This expression will work with the data presented above (minor edits to the accepted answer).
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
DATE(
YEAR(incidents[start_datetime]);
MONTH(incidents[start_datetime]);
DAY(incidents[start_datetime])
) + TIME(HOUR(incidents[start_datetime]);0;0);
incidents[end_datetime];
TIME(1;0;0)
);
"Expanded_incident_time"; [Value]
)
)
A more effectiv way would be to expand the incidents table so that each incident occurs for each time between the start and end datetime. This you can do with a calculated table like this:
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
incidents[start_date];
incidents[end_date];
TIME(h;m;s)
);
"Expanded_incident_time"; [Value]
)
)
Change the TIME(h;m;s) to the time resolution of your choosing.
Then make a relationship between your dateTime dim table and the [Expanded_incident_time] column (this should be a 1:*). Then you only have to make a measure which counts the number of rows in the new table. This will give you the number of active incidents during a specific time and I believe it will be faster than reitterating over the table each time.
I have a Table with a measure calculating difference of a KPI across a unique combination of three categories. I am trying to rank the measure that is calculating the difference but I get repetitive ranks although the value being ranked is different.
Table:
What I tried:
Rank Measure =
CALCULATE(
RANKX(
ALLSELECTED(Dim106),
[Difference between spend in region and store],,
DESC
)
)
[Difference between spend in region and store] = StoreTurnover - RegionTurnover.
RegionTurnover = CALCULATE(SUM(Dim106[Turnover |EJR|]),ALL(FactStore[Store ID]),ALL(FactStore[SOE]),ALL(FactStore[SOM]),ALLSELECTED(FactStore[Region]),ALLSELECTED(FactStore[YearMonth]),ALL(Dim106[WGI]),all(Dim106[Item Family]),ALL(Dim106[Item Subgroup]),ALL(Dim106[WGI Desc]),ALL(Dim106[Item Subgroup Desc]),ALL(Dim106[Item Family Desc]),ALL(Dim106[UniqueKey]))
StoreTurnover = CALCULATE(SUM(Dim106[Turnover |EJR|]),ALLSELECTED(FactStore[SOM]),ALLSELECTED(FactStore[SOE]),ALLSELECTED(FactStore[Store ID]),ALLSELECTED(FactStore[YearMonth]),ALLSELECTED(Dim106[Store]),ALLSELECTED(Dim106[Month]),ALL(Dim106[WGI]),ALL(Dim106[Item Subgroup]),ALL(Dim106[Item Family]),ALL(Dim106[WGI Desc]),ALL(Dim106[Item Subgroup Desc]),ALL(Dim106[Item Family Desc]),ALL(Dim106[UniqueKey]))
I have a fact table which has a higher hierarchy of store and and month, it has a crossfilter both directions relationship
Richt click on your dataset fields New Column and use the following expression:
Rank Column = RANKX('YourTable';'YourTable'[YourColumn];;ASC)
UPDATE:
Maybe this suits your scenario:
RankingBySale = Rankx(All(Table1[SalesID], Calculate(Sum(Table1[SalesValue])), , Asc, Dense)