SUMX in Stacked Column chart - powerbi

I'm hoping for some help with my measures. My data is quite sensitive so it's not easy to share. I've attempted to mock up as sales so hope it makes sense.
https://1drv.ms/u/s!Ap6q8W-mvm27g-dWehgkV6-p33VVsA?e=bGj3nV
I have written a measure to count the number of resales by TransactionID. I then use a sumx to total the resales by Transaction ID.
I've added this measure to a matrix against Month for Banding and this shows the correct row and column totals.
In the FACT Salestable, I have a field 'Tier'. When I attempt to add 'Tier' to a stacked column chart using the same measures, the data is incorrect. I think it is because the sumx is losing the filter created in the first measure but I don't really understand how to rectify this and have been trying for days!
Is anyone able to identify where my measures are incorrect - Id like to try to understand what exactly I'm asking the measure to do so I know where I'm going wrong for future work! Any help would be very much appreciated - thank you!
MEASURE 1:
CountResales =
Var _Cust = Max (FACTSales[CustomerID])
Var _Date = CALCULATE(min(FACTSales[DateOrigSale]),ALLSELECTED(FACTSales),FACTSales[CustomerID]=_Cust)
Var _ID = CALCULATE(min(FACTSales[CohortID]),ALLSELECTED(FACTSales),FACTSales[CustomerID]=_Cust,FACTSales[DateOrigSale]=_Date)
Var _CountResales =
CALCULATE(DISTINCTCOUNT('DIMReSales transactions'[TransactionID]),
VALUES('DIMReSales transactions'[TransactionID]),'DIMReSales transactions'[CohortID]=_ID,
'DIMReSales transactions'[New Sale]=1)
Return
_CountResales
MEASURE 2:
SumResales = Sumx(values('FACTSales'),[CountResales])

Related

Get a Department Percent Contribution of Total Sales

Table = opportunity
Dimension Field = Territory
Measure Field = AnnualSales
I want a measure that will take a territories SUM(AnnualSales) and see what percentage it makes of All territories SUM(AnnualSales) would be. I have not been able to accomplish this based off the articles I've read so far. Any help is greatly appreciated
My results would look something like this.
If you want to see results in a table of visualization:
create this measure: CALCULATE(SUM(AnnualSales))
then, drag Territory field in a table, add the new measure created in the table and then go to visualization editor right click on measure and click on show results as a percent of column total.
Waiting your answer ... :)
Regards
I was able to accomplish what I set out with the following.
cm_TotalEstAnnualSales = Calculate(Sum(opportunity[Est. Revenue (Annualized)]),All(opportunity))
cm_PctofTotalEstAnnualSales = Divide(SUM(opportunity[Est. Revenue (Annualized)]),[cm_TotalEstAnnualSales],0)
Thanks for the tips here!

PowerBI: Aggregate Measure correctly by condition on DATEDIFF

I have the following Table:
BaseTable
It represents processes with a certain category.
And there is also a Date Table over column TIMESTAMP.
I would like to show a Measure based on another Measure that calculates the Date-Difference until the selected Date.
So first this is how I calculate the Date-Difference:
AGE =
VAR SELECTED_DATE = CALCULATE(MAX(DATUM[Date]), ALLSELECTED(DATUM))
VAR STARTDATE_PROCESS = Calculate(MAX(Workflow[MIN_TIMESTAMP]),DATUM[Date]<=MAX(DATUM[Date]), ALL(DATUM[Date]))
RETURN
DATEDIFF(STARTDATE_PROCESS,SELECTED_DATE,DAY)
Now I want to use a Measure which depends on the result of AGE, like
NEW = IF([AGE]<=3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
or
OLD = IF([AGE]>3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
The Measures AGE, OLD and NEW look like that with the Base Table:
Measures
As you can see the aggregation is not working correctly:
Result_Wrong
But it should be like that
Result_Correct
Any idea how to fix that?
Thank you!
So the problem is that the subtotal is calculated at a whole different context, and because your Age measure is based on the MAX(Workflow[MIN_TIMESTAMP]) that won't take into account that there can be multiple processes.
To do what you want, you need to change the New and Old measures to perform an aggregation per process and then return the result of that. Something like this:
New_agg =
VAR tbl = ADDCOLUMNS(CALCULATETABLE(VALUES(Workflow[Process]), ALL('Date')), "age", [Age], "count_process", CALCULATE(COUNT(Workflow[Process]), ALL('Date')))
RETURN SUMX(tbl, IF([age]<=3, [count_process]))
Demo File
Let me know if below solution is working
Unfortunately I am unable to generate the dummy data that you have been using, so Created my own data for developing the solution.
Now from this data I have calculated the difference of dates and put it as Age
Now to get the count of process for the condition like yours, I have created two formulas and the result is:
Logic I followed here is, instead of creating measure I have created columns and took the sum of those columns which will give the data you need as sum of those columns.
Column for New:
New = IF((Sheet1[Age]) > 20, 1,0)
Column for Old:
Old = IF((Sheet1[Age]) < 20, 1,0)
Now place both formulas in "Values" and take sum as the aggregation.
Final result is

Consolidate filter and measure?

Is there a better way of trying to filter a table and apply a average to the filtered results?
Currently research online articles has brought me to creating a virtual table (CALCULATETABLE) and then a separate measure to AVERAGE the column value I require.
Filtered Table below
filtered_table = CALCULATETABLE ('ReportRawFigures',
ReportRawFigures[days_since_completed] < 29,
ReportRawFigures[3rd_party] = "Bloggs",
ISBLANK(ReportRawFigures[time_to_complete]) = FALSE(),
ISBLANK(ReportRawFigures[last_confirmed_issue]) = FALSE(),
ReportRawFigures[issue_status] = "")
Then a simple measure added:
average = AVERAGE(filtered_table[column])
I'm not very bright... I figured it out.
You create the filtered table, then when you build you visual you simply select the column and chose to average its output..
Hope this helps anyone looking like I first did.

In Power BI, how can you return the category label based on the max value for all categories returned by a measure using DAX?

Looking for some DAX guidance here, to return a text field based on the maximum returned by a measure appplied over that column.
i.e we have table[category_column] and [measure]
and the measure references columns in other tables, with two-sided relationships.
Any help much appreciated, very stuck!
Thanks in advance
If I'm reading right, I think you want to draw the measure against the column values and return the row with the max of the measure. Something like this?
newMeasure =
VAR vals = SUMMARIZE('Table', 'Table'[Column1], "myMeasure", [Measure])
VAR measureMax = MAXX(vals, [myMeasure])
VAR value = CALCULATE(MAXX(FILTER(vals, [myMeasure] = measureMax), [Column1]))
RETURN
value
If your column to be used for max is Column1 and your text field is Column2 something like the below could work:
Measure =
VAR Max_Value =max('Table'[Column1])
Return CALCULATE(MAX('Table'[column2]),'Table'[Column1]=Max_value)
If this is not what you are looking for you have to explain your requirements in further detail, hopefully with examples.

Automatic Data Transfer to DAX

I'm new in this so please pardon me for such a basic question.
I'm trying to pass some dates as filters to a measure.
I have a simple measure that counts opened items "last week".
The DAX I used:
OpenedLastWeek = CALCULATE(COUNTROWS(Table);Table[Created
Year]=2018;Table[Created Week]=45)
But there should be a way to pass values to the DAX code to replace manually entered "2018" and "45".
I tried Date Tables, extra tables with dates in it... I'm just confused.
Thanks.
In this case, I would probably use variables.
OpenedLastWeek =
VAR CreatedYear = --<Desired Year Calculation>--
VAR CreatedWeek = --<Desired Week Calculation>--
RETURN
CALCULATE(
COUNTROWS(Table);
Table[Created Year] = CreatedYear;
Table[Created Week] = CreatedWeek
)
Basically, you define the week and year how you'd like and then pass those variables into the CALCULATE function.
Thank you very much. It solved my problem.
For this case, I tried variables before but I was getting wrong results. I guess that was because I was using "CountA()" instead of "Countrows()".
Thanks again.
Have a nice day,
Eld