How to get a value from a Range? - Power BI - powerbi

I have data in a table and depending on their sale I have to assign a cost:
For example, if have a sale of 200,000 my cost will be 61,298.
I have tried to make a measure to get this but with my current formula, I only get the Sum of all of the values of Cost.
Cost of Sale = CALCULATE(SUM('Cat_VEA0 Cost'[Cost]),
FILTER('Cat_VEA0 Cost',
[Sales] >= MIN('Cat_VEA0 Cost'[From]) &&
[Sales] < MAX('Cat_VEA0 Cost'[To])))
So I don't know, how to get the value of Cost, depending on which range of Sales it is each shop.

You can use MAXX() supposing you want to match only one row in 'Cat_VEA0 Cost' and you want the row with the maximum price.
Solution
Cost of sale = MAXX(
FILTER(
'Cat_VEA0 Cost',
AND(
'Cat_VEA0 Cost'[From] <= SUM(Sheet2[Sales]),
SUM(Sheet2[Sales]) < 'Cat_VEA0 Cost'[To]
)
),
[Cost]
)
Explanation
Get the maximum [Cost] from all the rows that meet the condition:
MAXX(..., [Cost])
Set the condition to [From] <= SUM([Sales]) <= [To]:
FILTER(
'Cat_VEA0 Cost',
AND(
'Cat_VEA0 Cost'[From] <= SUM(Sheet2[Sales]),
SUM(Sheet2[Sales]) < 'Cat_VEA0 Cost'[To]
)
)

Related

Cumulative total percentage in Power BI

I have a summary table in Power BI which shows how many days it takes for leads to convert to a sale. It has 2 columns, sum_convert (the amount of days in between lead creation date and converted date) and count_lead (the count of leads that have taken that amount of days to convert), both are numeric values. Here is an example of the data:
What I want, is a column next to count_lead that shows the running percentage total in the specific ascending order of sum_convert. Currently I've created a measure called lead_count which is the sum of count_lead. Then I've attempted to create the cumulative total with the following measure:
Cum_Lead = calculate([lead_count], FILTER(ALL(Convert_Count_Summary[Sum_Convert]), SUM(Convert_Count_Summary[count_lead]) <= [lead_count]))
This creates a cumulative total, but not in the specific sum_convert order, it's in the order of largest volume for count_lead. Any idea what I need to change so that it's in the order of sum_convert?
You could do this in Power Query using M:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)), type number)
Or as a calculated column using DAX:
Cumulative Count DAX =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
Edit:
Cumulative percentages in Power Query:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_Percent_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)) / List.Sum(#"Previous Step"[count_lead]), Percentage.Type)
Cumulative percentages calculated column in DAX:
Cumulative Count % DAX =
VAR _Numerator =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
VAR _Divisor =
SUM ( Convert_Count_Summary[count_lead] )
RETURN
DIVIDE (
_Numerator,
_Divisor
)

Power BI - Conditional Formatting per Row in Matrix Visual

I'm trying to use conditional formatting for the colours of a matrix. The goal is that for each row the colour of the cell will depend on it being above or below the average for that particular row.
I have managed to produce the following matrix using this DAX measure as the conditional formatting:
Conditional Formatting =
VAR SelectedMonth = CALCULATE(SUMX(Reviews_Table,Reviews_Table[Number_of_Reviews]))
VAR AveragePerCountry = [Average Reviews]
RETURN
SWITCH(
TRUE(),
SelectedMonth >= 1.5*AveragePerCountry, "#008651",
AND(SelectedMonth < 1.5*AveragePerCountry, SelectedMonth >= AveragePerCountry), "#82ac2b",
AND(SelectedMonth < AveragePerCountry, SelectedMonth >= 0.5*AveragePerCountry), "#ff8100",
SelectedMonth < 0.5*AveragePerCountry, "#FE2828" )
where [Average Reviews] measure has been defined as
Average Reviews =
VAR SUMFILTER =
CALCULATE (
SUM ( Reviews_Table[Number_of_Reviews] ),
ALLSELECTED( 'Reviews_Table' )
)
VAR COUNTFILTER =
CALCULATE (
DISTINCTCOUNT ( Reviews_Table[Number_of_Reviews] ),
ALLSELECTED( 'Reviews_Table' )
)
RETURN
DIVIDE ( SUMFILTER, COUNTFILTER )
However, as it stands, this conditional formatting seems to compare each value to the total average over all rows. Is it possible to obtain the average for each row (in this case "Market") and use that as conditional formatting? Hopefully the answer will respect filters (I have a slicer that changes the time interval, and the average should be for that specific interval).
All help would be appreciated!
PS: Just as additional info Reviews_Table has the following columns: Month, Market, Retailer (not depicted in the matrix but accessible by pressing "+") and Number_of_Reviews.

How to create a cumulative sum over two columns with comlementary Date Range in Power BI

I'm trying to build a line Chart to show the cumulative sum of the Forcast. I have two Tables, one for the Actuals and one for the forcast, both linked to a Date table. The Chart should show the cumulative sum of the actuals till current month and from next month on those from the Forcast.
I've created so far the following measures to get the Forcast Chart:
Chart_Forecast not cumulated =
VAR Actual_Hrs_not_cumulated = CALCULATE([Total Actuals],FILTER(IN_ACTUALS, IN_ACTUALS[Date] <= MAX(CurrentMonth[CurrentMonthParameter])))
VAR Forecast_not_cumulated = CALCULATE([Total Forecast],FILTER(IN_Forecast, IN_Forecast[Date] > MAX(CurrentMonth[CurrentMonthParameter])))
RETURN
IF((SELECTEDVALUE('LT_Reporting Calendar'[Date]) <= MAX(CurrentMonth[CurrentMonthParameter])),Actual_Hrs_not_cummulated, Forecast_not_Cumulated)
This one gives me the line chart of the non cumulated Forcast, and it works.
But as soon as I want to build the cumulative sum on the measure above according to the measure below, I get only the cumulative sum till current month and the future is omitted. I think I have a filter issue.
I've tried many methods of building a cumulative sum, always getting the same result.
Chart_Forcast Cumulated =
CALCULATE(
[Chart_Forecast not cumulated],
FILTER(
ALL('LT_Reporting Calendar'[Date]),
'LT_Reporting Calendar'[Date] <= MAX ('LT_Reporting Calendar'[Date])
)
)
Input:
Result: The Chart
Does any body have an Idea on this?
Many Thanks in advance
I'd suggest writing it as a sum like this:
Cumulative =
VAR ParameterDate = SELECTEDVALUE ( CurrentMonth[CurrentMonthParameter] )
VAR AxisDate = SELECTEDVALUE ( 'LT_Reporting Calendar'[Date] )
RETURN
CALCULATE (
[Total Actuals],
'LT_Reporting Calendar'[Date] <= AxisDate,
'LT_Reporting Calendar'[Date] <= ParameterDate
) +
CALCULATE (
[Total Forecast],
'LT_Reporting Calendar'[Date] <= AxisDate,
'LT_Reporting Calendar'[Date] > ParameterDate
)

Create measure to calculate rows based on TO and FROM figures in another table

I have a report that fetches weekly data. I have a weeks table that contains the weeks and dates and relationships set up with the below tables.
I have a QueueSummary table;
I have ConciergeHours table;
The concierge hours differ from day to day, so i keep the start and finish time in a table so we can update it dynamically.
I am trying to create a measure to calculate how many interactions we have had, between the hours in the ConciergeHours table. Keep in mind this is a weekly report, so on any given day during the week, the hours may be different.
The calculation we are after is;
Number interactions accepted on queue LinkConciergeVQ (QueueSummary table) between the starthour and endhour in ConciergeHours table MINUS Number interactions offered on queue LinkVQ (QueueSummary table) between the starthour and endhour in ConciergeHours table
I currently have the measure looking like this. It works, but the between hours are not respected correctly, which i understand would be a result of the MIN, MAX.
m_deflections =
CALCULATE (
SUM ( QueueSummary[s_accepted] ),
QueueSummary[queue] = "LinkConciergeVQ",
filter(QueueSummary, [hour] >= min(ConciergeHours[starthour])),
filter(QueueSummary, [hour] < max(ConciergeHours[endhour]))
)
- CALCULATE (
SUM ( QueueSummary[s_offered] ),
QueueSummary[queue] = "LinkVQ",
filter(QueueSummary, [hour] >= min(ConciergeHours[starthour])),
filter(QueueSummary, [hour] < max(ConciergeHours[endhour]))
)
I hope i've explained this correctly.
If anyone could offer any help!?
Thanks so much
Not sure I understand your table relationships, but if its 1:* from ConciergeHours to QueueSummary via date then I think you can use this measure, or at least it puts you on the right track, I hope:
m_deflections =
var s_accept =
SUMX(
'ConciergeHours',
var startH = 'ConciergeHours'[startHour]
var endH = 'ConciergeHours'[endHour]
return
CALCULATE(
SUM('QueueSummary'[s_accepted]),
'QueueSummary'[hour] >= startH && 'QueueSummary'[hour] < endH
)
)
var s_offered =
SUMX(
'ConciergeHours',
var startH = 'ConciergeHours'[startHour]
var endH = 'ConciergeHours'[endHour]
return
CALCULATE(
SUM('QueueSummary'[s_offered]),
'QueueSummary'[hour] >= startH && 'QueueSummary'[hour] < endH
)
)
return
s_accepted - s_offered
I would collapse the 2 pairs of FILTER functions into a single FILTER for each CALCULATE. That will apply the filters against starthour and endhour in combination (the && means AND), where I expect they are currently applying as OR logic so not having any effect.
Along the lines of this:
m_deflections =
CALCULATE (
SUM ( QueueSummary[s_accepted] ),
QueueSummary[queue] = "LinkConciergeVQ",
FILTER (
QueueSummary,
[hour] >= MIN ( ConciergeHours[starthour] )
&& [hour] < MAX ( ConciergeHours[endhour] )
)
)
- CALCULATE (
SUM ( QueueSummary[s_offered] ),
QueueSummary[queue] = "LinkVQ",
FILTER (
QueueSummary,
[hour] >= MIN ( ConciergeHours[starthour] )
&& [hour] < MAX ( ConciergeHours[endhour] )
)
)

Calculated Table Cumulative Total

I'm trying to add a column with a cumulative total in a calculated table with this DAX:
CALCULATE(sum('Eighty20'[LTV]), FILTER( ALLSELECTED( Eighty20 ), Eighty20[Rank Asc] <= MAX (Eighty20[Rank Asc])))
However, I'm getting the overall sum in each field rather than the cumulative total. Any ideas why?
The reason you get the total is that you filter based on :
Eighty20[Rank Asc] <= MAX (Eighty20[Rank Asc])
This returns all the rows because it checks if the current Rank Ac <= max, what is always true. To get cumulative:
yourColumn =
var curRank = Eighty20[Rank Asc]
return CALCULATE(sum('Eighty20'[LTV]), FILTER(Eighty20, Eighty20[Rank Asc] <= curRank))