Show only weeks with data using ALLSELECTED function in DAX - powerbi

I'm trying to get a simple chart with columns for one value and a line for another value.
I'm using a calculated calendar to show the week/year item with an
order.
A table with all the data and a calculated measure in DAX to show
the acumulated for every week.
My problem is that the DAX measure is forcing all the weeks to show in the chart, even the ones without data (because they will get the acumulate from the ones before).
This is the measure:
P-Soll(sum) = CALCULATE(
SUM(TEVON_parts_current_K[P-Soll]),
ALLSELECTED(TEVON_parts_current_K),
'Orden Semanas_Soll'[Index]<=MAX('Orden Semanas_Soll'[Index])
)
This is how it shows:
And these are the weeks with data:
There are more, but you can get the idea. Is there any way to show only the weeks with real data?

try If like
P-Soll(sum) =
VAR CurrentWeekValue = ...
VAR Cumulative=
CALCULATE(
SUM(TEVON_parts_current_K[P-Soll])
,ALLSELECTED(TEVON_parts_current_K)
,'Orden Semanas_Soll'[Index]<=MAX('Orden Semanas_Soll'[Index])
)
RETURN
IF(CurrentWeekValue = 0,BLANK(),Cumulative)

Related

The PeriodCustomer column is not found or cannot be used in this expression. Power BI (DAX)

I am writing code to determine the Running Total Customers: All Customers Until This Period. Now, I have created a This period's Customers table to contain the following code:
PeriodeKlant = DISTINCTCOUNT(Text[PCHN])
I have now created the code as below:
Running Total Customers =
var _currdate=MAX(Tekst[Datum].[Date])
var _salesthisperiod=Tekst[Verkoopdoc]
return
if(_salesthisperiod>0,
CALCULATE(
[PeriodeKlant],
FILTER(
ALLSELECTED(Tekst[Datum].[Date]),
ISONORAFTER(Tekst[Datum].[Date], _currdate, DESC)
)
)
)
I get the message that the previously created column cannot be used, this column has the value integer and at summary it says sum. I don't know if that is why I would not be able to load this data?
But because of this error I can't run my measure.
The way your DAX is written, it's expecting [PeriodeKlant] to be a measure rather than a calculated column. Try deleting this calculating this column and defining it as a measure instead.

need assistance\ideas on building what is hopefully a simple DAX measure

I am trying to figure out the DAX to create a new measure in an SSAS Tabular data model. An example of what I am trying to do is more easily shown than described. My SSAS Tabular dataset produces the following table. Cols A and B are from the stores table, Col C is a measure from the Sales table, Col D is a measure from the Products table, and Col E is C/D. This all works fine. Data has been mocked up in Excel to protect the innocent, but it is working in Power BI.
What I would like to do is add a new measure which calculates the Sales/Product at the state level and have that measure show for each store in that state, as shown below
Presumably I have to iterate over all rows and calculate the total sales/state and total products sold/state and divide those 2 to get the answer, but can't work out the DAX to get there. I have tried numerous combinations of
calculate(
sumx(...),
filter(
all(...),
...
)
)
to no avail.
You should use FILTER with ALL to manipulate a context(remove current context);
MesureSumStateLevel = calculate(SUM('Table'[Amount]),
FILTER(ALL('StoreStateTab'), 'StoreStateTab'[State] =
SELECTEDVALUE('StoreStateTab'[State])))
https://dax.guide/filter/
https://dax.guide/selectedvalue/
https://dax.guide/all/
Thanks for the tip. I originally tried that and dropped it because I couldn't get it working. I revisited this morning and solved it. Here is what I did:
State Ttl =
var trxYr = convert(SELECTEDVALUE(dim_date[Year]), INTEGER) //needed because Year is stored as text in the model
var trxMo = SELECTEDVALUE(dim_Date[Short Month Name])
var trxState = SELECTEDVALUE(fact_Sales[state])
Return
CALCULATE(
SUM(fact_sales[SalesAmt])
,all(fact_sales)
,year(fact_sales[SaleDATE]) = trxYr
,dim_Date[Short Month Name] = trxMo
,dim_Stores[state] = trxState
)

Running totals from a fact table displayed by values from a linked date table

I have a mutation table on which I want to have a running total. This measure I can create succesfully as long as I present the data with the date of the mutation table.
However, the dates of the mutations are linked to a time_dimension table which contains, specifically relevant for this example, isoweek numbers.
When I try to present the data by these week numbers, the running total no longer works.
The data model:
I tried toggling filter directions in the data-model (which I shouldn't do), and working with the time_dim in the DAX code, but to no avail.
The code:
Balance =
VAR MaxDate = MAX ( AV_Mutations[Date])
RETURN
CALCULATE(
SUM(AV_Mutations[Quantity]),
AV_Mutations[Date] <= MaxDate,
ALLSELECTED(AV_Mutations[Date])
)
The data:
Table 1 shows that the code above goes wrong when applying week_numbers. It is in this format I want to present the data.
Table 2 shows that the code works when using date field from the Mutation table.
Table 3 shows the relation between mutation and date_dim works, but this should be obvious.
Can anyone help me or nudge me in the right direction calculating a running total over YearWeek_Number?
I think you're using the wrong date column in your measure.
Try something like this:
Balance =
VAR MaxDate = MAX ( AV_Mutations[Date])
RETURN
CALCULATE(
SUM(AV_Mutations[Quantity]),
Time_Dim[Full_Date] <= MaxDate,
ALLSELECTED(Time_Dim[Full_Date])
)

DAX - Grand Total not adding up to Row Total

I have a powerbi report which is running a dax formula to calculate a custom measure. In the picture below, the total at the bottom doesn't seem to add up to the individual rows. I've been trying my luck for some time and can't seem to figure out why this is.
The DAX formula used is as follows
SumRest<24hrs7Day = CALCULATE(
DISTINCTCOUNT(WorkTimeDirective[EmployeeKey]),
FILTER(
ADDCOLUMNS(
SUMMARIZE(WorkTimeDirective,Employee[EmployeeKey],'Date'[DateKey]),
"totRestHrs", CALCULATE(MAX(WorkTimeDirective[RestHours])
,DATESINPERIOD('Date'[DateKey], LASTDATE('Date'[DateKey]), -7, DAY))
),
[totRestHrs]<24
),
WorkTimeDirective[IsEmployeeAbove18]=1
)
Any idea why this is and what I am doing wrong.
For using SUMX the main step is listing the values which you are iterating over, which it typically a table or column. In this case it sounds like you would do a column.
For the example I just had it call the measure you already defined, since breaking DAX calculations into smaller pieces makes writing/testing complex formulas easier.
The idea being that it would iterate over the unique values which are in your TableName[Site Name], then run the [SumRest<24hrs7Day] under that context. I used TableName for the table due to not knowing the name for the table.
SUMX_Example = SUMX( VALUES( TableName[Site Name], [SumRest<24hrs7Day])

Power BI Rolling Average

I've used the new Quick Measures feature of Power BI to build a 3 month rolling average calculation and it's working well. The equation is displayed below. However, when I try to use this metric in a time series visualization, the calculations are displaying three months past the current month, but I'd like for the calculation to stop at the current month.
I've played around with the __DATE_PERIOD variable to no avail. My date filter for the page is set to show all dates in the current months or 12 months prior via a calculated column on the date table.
Is anyone aware of how I can get the visualization to end at the current month?
Average Days to Close Rolling Average =
IF(
ISFILTERED('Date'[Date]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
VAR __LAST_DATE =
ENDOFMONTH('Date'[Date].[Date])
VAR __DATE_PERIOD =
DATESBETWEEN(
'Date'[Date].[Date],
STARTOFMONTH(DATEADD(__LAST_DATE, -3, MONTH)),
__LAST_DATE
)
RETURN
AVERAGEX(
CALCULATETABLE(
SUMMARIZE(
VALUES('Date'),
'Date'[Date].[Year],
'Date'[Date].[QuarterNo],
'Date'[Date].[Quarter],
'Date'[Date].[MonthNo],
'Date'[Date].[Month]
),
__DATE_PERIOD
),
CALCULATE(
'Closed Opportunities'[Average Days to Close],
ALL('Date'[Date].[Day])
)
)
)
In order to limit what is displayed within your chart, you need to filter the applicable date field so it only displays the dates you desire. In this case, you only want it to include dates <= today.
In order to automatically filter it when it is refreshed, I typically add a custom DAX column to the date table that I can filer on. In this case it would be something along the lines of:
excludeFutureDatesInd = 'Date'[Date] <= TODAY()
You can then add a visual, page, or report filter selecting all dates where [excludeFutureDatesInd] = True.
Not sure if you're still having issues with this, but I'd like to share a hack fix for those landing here. I fixed this issue by filtering on the base data (in your example, this would be "Average Days to Close"). Set a visual-level filter to include only those items where Average Days to Close > 0, and you should get the extra dates cut off the end of the graph.
So long as all of your base data passes through the filter, you should be good.