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

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
)

Related

Show only weeks with data using ALLSELECTED function in DAX

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)

Power BI Min date of each category

I am familiar with SQL and I can write a query to return results of a query to Select MIN(Date), MAX(Date), SUM(quality) and GROUP BY. However, I am new to Power BI and DAX and find it difficult to do the same on Power BI. Below is my situation.
These tables on Power BI:
Dim_ManefactureDate
Dim_ReleaseDate
Fact_OrderID
Table Relationships
Adding a table visualization to a new page to show data from three tables above, data is showing as below:
Under Values of Visualizations, when selecting SUM over Netweight, it automatically summarizes expected Netweight. However, for ManufactureDate and ReleaseDate, when selecting Earliest then Power BI table shows unexpected 1/01/1900 values like this:
I expect earliest date of each OrderID as below:
I have also tried to use a DAX function to create a new column but it gets error
ManufactureDate_Earliest =
VAR Sum_Netweight = SUM(Fact_OrderID[NetWeight])
VAR GroupBy_OrderID = GROUPBY(Fact_OrderID,Fact_OrderID[OrderID])
RETURN
CALCULATE(
MIN(RELATED(Dim_ManufactureDate[DateBK]))
)
Thank you very much for your help
Due to getting values from relationship tables, used these measured and solved the issue
ManufactureDate_Earliest =
CALCULATE(
MIN(ManufactureDate[DateBK]),
CROSSFILTER(Fact_Order[ManufactureDate_DateSK], ManufactureDate[DateSK], BOTH)
)
ReleaseDate_Earliest =
CALCULATE(
MIN(ReleaseDate[DateBK]),
CROSSFILTER(Fact_Order[ReleaseDate_DateSK], ReleaseDate[DateSK], BOTH)
)

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.

Power Bi DAX: Cumulative Sum not Showing Correct Data

I am having trouble making a cumulative sum.
It is showing data from the previous column instead of being a cumulative.
I have tested it out on a test file and it works fine but using the same format of the formula:
QuoteValue running total in ClosedDate =
Sales running total in Date =
CALCULATE(
SUM('Sheet1'[Sales]),
FILTER(
ALLSELECTED('Sheet1'[Date]),
ISONORAFTER('Sheet1'[Date], MAX('Sheet1'[Date]), DESC)
)
)
Im not sure what i am doing wrong, ive used ALLSELECTED, FILTER(ALL, ALL
The Quotevalue and Date are both in the same table and all other fields im using in the visual are all in the same table.
Im stumped.
Here is a dummy sheet i have put together.
Dummy Sheet
Sales running total in Date =
var person = 'Sheet1'[Person]
var toDate = 'Sheet1'[Date]
return CALCULATE(
SUM('Sheet1'[Sales]),
FILTER('Sheet1', 'Sheet1'[Person] = Person && 'Sheet1'[Date] <= toDate)
)
What we do here is that for each row, we get the other rows where persons are same and date is lower or equal. From this we calculate the sum.

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])
)