I'm using Power Bi desktop to create a sales dashboard, and ran into a small code issue.
I need to calculate average sales in a period of working days, and tried:
TOTALYTD([Sales];
dimCalendar[Date];
dimCalendar[WorkingDays]<=16)
16 is the related working day for 22APR2019 in Brazil.
After that I plan to divide it by a month count or another similar method.
The hard coded 16 works very well, however when I try to use another formula instead:
TOTALYTD([Sales];
dimCalendar[Date];
dimCalendar[WorkingDays]=CALCULATE(MAX(dimCalendar[WorkingDays]);
LASTDATE(dimCalendar[Date])
))
It gives an error that I can't use calculate in a true/false expression.
I tried to use calculated column, but doesn't work as well.
Do you have any idea of how I can create this measure?
Related
I'm trying to figure out a solution to my problem. Basically we get a monthly report with about 3000 records and there's a bunch of reporting that is done on that, and there are calculations based on various columns. e.g.
Date
Total usage
Recommended reduction
Product
01.01.2022
1000
500
A
01.01.2022
1300
70
B
01.01.2022
2000
900
C
...
...
...
At the end of it Power BI kindly sums up the columns which is great, but now what I am trying to do is take the sum of these columns and store them in a summary table so that it would be something like this so that I could use it for a time series visual
Month
Sum Total Usage
Sum Recommended Reduction
January
59720
12040
February
81020
20580
...
...
...
I have no idea how to go about doing this. Is this the right way to go ? Or is there a way to create a visual without having to create a summary table ? I'm at a bit of a loss, so any suggestions would be really appreciated.
You don't need any DAX calculations for that. Simply pull your data onto the fields of a line chart visual like shown below. Note that you have to drill-down from Year to Month to actually see the lines.
I am trying to create a measure which will calculate the Total Project Revenue
while I have 2 different projects. For each project there is a different calculation:
for Hourly project the calculation should be: Income * BillHours
for Retainer project the calculation should be: Income*TotalWorkinghours
I wrote the below DAX:
enter code here : Total project revenue = IF(max(DimProjects[ProjectType])="Hours",
max(FactWorkingHours[Income])[BillHours],max(FactWorkingHours[Income])*
[Total Working Hours])
the rows are calculated correctly but the total in the table is wrong
what should I fix in DAX so the total of all raw will correct as well.
The total Revenue should be 126,403.33
Thank you in advance
you can find here the table with the results
It's hard to say exactly what your measure is doing because, as written here, that is not a valid measure. I pasted it into the DAX Formatter, which I would recommend for formatting and pasting here into code blocks, and the measure was invalid. It would also be helpful to post the other measures this measure references, eg. [Bill Hours] and [Income Hours].
That being said, I think I can kind of tell what's going on. Your total is probably wrong because the filter context at the total level is being based of the condition where:
MAX ( DimProjects[ProjectType] ) = "Retainer" (or some other value not in your shared snippet)
That is because when you consider the MAX of a string, the higher alphabetical order is considered. Therefore, "Retainer" > "Hours". So at the total level, your table is outputting—more than likely, I can't be certain without more information—the false condition of your measure:
MAX ( FactWorkingHours[Income] ) * [Total Working Hours])
There is a better way to handle your intended outcome. IF statements, in the way you are attempting to use it, are rarely used in a calculated measure. You may be better off trying a calculated column without using the MAX functions. Again, I can't give an exact code recommendation without more context. Hope that sends you in the right direction!
To preface this, I'm fairly experienced in Excel and VBA but new to PowerBI and more than a bit confused.
I have a flat table with a [creationdate]-, [Prio] (Priority (1,2,3)) and a calculated [Days Open] column, among many irrelevant others. I need to create a chart that displays the average days a case was open by priority of the case.
To display the average "days required" per (opening-) month for the past 18 months, I created the following measure:
Prio 1 = CALCULATE(AVERAGE('SourceName'[Days Open]),'SourceName'[Prio]=1)
Then I used that as a value, and used the [creationdate] as the x-axis. (Later I changed the x-axis to a new date table linked to [creationdate] without it making a difference.) To display this as monthly averages, I used the hierarchy limited to years and months, and went down one level in the chart.
Something seemed off so I checked first in Excel, then in the data source in PowerBI and yep: The averages in the PowerBI chart are complete bullshit.
Where did I go wrong? I assume it has something to do with the date hierarchy... So I created a date table as recommended (which....why?!) and linked it. That didn't make a difference.
Meanwhile in the data panel if I filter by the date column and calculate the average with the filtered selection of numbers externally, everything works as expected, so its not like there's a date formatting issue.
Do I have to create a calculated column with something akin to
DATE(YEAR([DateColumn]),MONTH([DateColumn]),1)
, then use that as the x-axis without the hierarchy, and hope nobody cares about the day in the label? Or is there something wrong with the measure used? I'm completely lost.
I have, in PowerBI (October 2017 release) some data showing the snapshotted output of some solar panels at five-minute intervals.
I presently have a measure which calculates the total production for each day by getting a sum of the instantaneous readings for the day and dividing by 12 (because there are 12 readings per hour) and then shows the average of this daily amount over whichever period is in the current context:
Avg Daily Production (KWh) =
AVERAGEX(SUMMARIZE('Solar Output', 'Solar Output'[Date], "Daily Production (KWh)", SUM('Solar Output'[Production (KW)]/12), [Daily Production (KWh)])
For my visualisations I'd like to show a chart which displays the average production at various times of the day compared to the production at that time for the best day, i.e. the day with the highest value for [Avg Daily Production (KWh)].
Here is my working so far:
Identify the top day by using
Best Day = TOPN(1, SUMMARIZE('Solar Output', 'Solar Output'[Date], "Daily Production (KWh)", [Avg Daily Production (KWh)]), [Daily Production (KWh)], DESC)
But this produces a row, not a scalar, and I can't quite work out how to just get this date and use it as a filter for a measure like this:
Production On Best Day (KW) =
CALCULATE(SUM([Production (KW)]), FILTER(ALL('Solar Output'[Date]), 'Solar Output'[Date] = [Best Day]))
The chart I want would look like this, with the average production for the selected dates in green and then another series showing the production at those times on the best day pencilled in red.
Can't really get my head around the calculations involved but I hope I get your requirement.
So you want to plot two lines on the same chart, one always being the data from the best day as reference, and the other one the data selected from the date filter, right?
You're actually pretty closed to the solution with the info given above.
I just modified your DAX to:
Production On Best Day (KW) =
CALCULATE(
SUM('Solar Output'[Average]),
FILTER(
ALL('Solar Output'[Date]),
'Solar Output'[Date] = VALUES('Best Day'[Date])
)
)
(Not quite sure how you compute your intermediate measures so you may need to adjust according to your measures)
Yes, TOPN() returns a table, so you can get the date value using VALUES('Best Day'[Date]) and use it for comparison.
Results:
Alternate approach to solve this issue is to use the MAXX() function to get a scalar value out of the the table returned by TOPN().
This works where MAX() doesn't because MAXX() accepts a table expression as its first argument (the table expression is the result of the TOPN() function) where MAX() only takes columns from existing tables.
Best Day = MAXX(TOPN(1,
SUMMARIZE(ALL('Solar Output'),
'Solar Output'[Date],
"Daily Production (KWh)",
[Avg Daily Production (KWh)]),
[Daily Production (KWh)],
DESC),
[Date])
The benefit of this approach is that it should work in versions of DAX with no calculated table support (e.g. SSAS 2014, Excel 2013).
I would like to create a calculated column or measure that shows the time elapsed in any given month. Preferably as a percentage.
I need to be able to compare productivity (rolling total) over a month's period between different months.So creating a percentage of time passed in a month would put every month on a level playing field.
Is there a way to do this?
Or is there a better way to compare productivity between 2 months on a rolling basis?
EDIT
I am graphing sales on a cumulative basis. Here is a picture of my graph to demonstrate.[][
Ideally I would like to be able to graph another person's sales on the same graph for a different month to compare.
The problem is each month is different and I don't think power bi allows much customization of the axes.
So I figured a potential solution would be to convert months to percentages of time passed, create two separate graphs and place them on top of each other to show the comparison of sales.
Using percentages doesn't sound right here: one person's "productivity" in February will appear lower than another person's productivity in March just because February has 3 less days.
Just use [Date].[Day].
To answer the original question (even though it shouldn't be used for this), month progress percentage calculated column:
MonthProgress% =
var DaysinMonth = DAY(
IF(
MONTH(MyTable[date]) = 12,
DATE(YEAR(MyTable[date]) + 1,1,1),
DATE(YEAR(MyTable[date]), MONTH(MyTable[date]) + 1, 1)
) - 1
)
return MyTable[date].[Day]/DaysinMonth*100
Also check DAX functions PARALLELPERIOD and DATEADD.
This is the solution I settled on.
I customized the ranges for the x and y axes so they match.
For the y-axis, I simply put the range from 0 to 50+ our highest month.
For the x-axis, I created a column with the DAY function so I got a number assigned to each day of the month which allowed me to manually set the chart range from 0 to 31. I have asked another question on how to only get workdays.