I was hoping someone could help me with a part of the formula to get me to my end goal.
Table PR_HIST_MOVIM_PEDID:
Table LOG:
PR_HIST_MOVIM_PEDID table has order status per day, but when you turn the month, it loses information. A LOG table has one last transition of each request per day and it is this table that is the correct reference. I have to create a new table that shows me all the requests and their days without losing information as soon as the birthday turn-over happens.
Example:
NOTE: Notice that this is a small example. My database table has tens of thousands of values.
How to achieve the final result?
Table Log
First, create a new table that just has the dates that you want to use. I created a new table like this:
AllDates = CALENDAR(MIN(TableLOG[DTH_INCLUI_LOG]), MAX(TableLOG[DTH_INCLUI_LOG]))
Now write a measure like this:
Measure =
VAR CurrentDate = SELECTEDVALUE(AllDates[Date])
VAR MaxDate = CALCULATE(MAX(TableLOG[DTH_INCLUI_LOG]),
TableLOG[DTH_INCLUI_LOG] <= CurrentDate)
RETURN CALCULATE(MAX(TableLOG[VLR_NOVO]), TableLOG[DTH_INCLUI_LOG] = MaxDate)
You should now be able to create a matrix with the AllDates[Date] for the columns and the Ovitem_log for the rows with the Measure for the values.
Related
I have run into an interesting use case that has been causing some headaches.
Basically, the end user wants to be able to click on the month/category on a stacked column chart, drill through to show all items in that category, but then also show the values for every month.
The data model basically has two tables. One contains all the transactional detail (Table A).
The second table contains a monthly snapshot (Table B), with both being linked by an item-month key.
The snapshot contains the categories for each item/month combination. There is also a category on the snapshot for table B, "Idle", which by definition would not have values for the specific month on Table A.
So basically to frame it another way, I am wanting a measure that will do something like this:
For all items in the selected month/category of Table B, return all values from Table A.
Because the resulting visual will be drillthrough, I think it also will basically guaranteed be filtered by the status/month.
Any help on this would be appreciated, would be happy to clarify at all.
This is the code I had come up with so far. It appears to work in cases where data exists, but not situations where the aforementioned Idle items appear. (ex: an Idle Item may have values for 11/12 months, but trying to drillthrough on the period where it does not have values results in slicers not working on the drillthrough page, which may be another issue altogether)
The "DistinctDates" table is a disconnected date table containing each distinct year/month from table B.
Measure =
VAR MaxDate = MAX(DistinctDates[DATE])
VAR MinDate = MIN(DistinctDates[DATE])
Var Output=
CALCULATE(
SUM(TABLE_A[VALUE])
, FILTER(
ALLEXCEPT(TABLE_B, TABLE_B[ITEM]),
TABLE_B[DATE] >= MinDate
&& TABLE_B[DATE] <= MaxDate
&& NOT ISBLANK(SELECTEDVALUE(TABLE_B[ITEM]))
)
)
Return Output
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)
)
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
)
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])
)
I have data from multiple countries on a monthly basis. Since the updates are not regular, I want to set up filter to visuals, so they would show the last month for which I have data from all the countries. I have data from each country loaded into a separate dataset, which then are merged into one big. Is there an easy way to place such filter? I managed to use "LASTDATE" function in each of country sets to find which date is last, but if I try to filter with that measure, I simply get nothing in a result. Thanks!
Well, this feels a little clunky to me but I believe it will work for you. There are two steps. The first is to create a summary table that reads through your data and counts the number of distinct countries that you have in each month. This will be a new table in your model, so go into the modeling tab, click 'New Table' and add this DAX. Obviously, correct for your table and column names.
SUMMARIZED_ROWS = SUMMARIZE(
'Table1'
,Table1[Month]
,"CountOfCountries"
,DISTINCTCOUNT(Table1[Country])
)
Now add a measure to the table (or anywhere) like this:
MonthWithMostCountries = CALCULATE(
LASTNONBLANK(SUMMARIZED_ROWS[Month], 1 )
, FILTER(SUMMARIZED_ROWS, SUMMARIZED_ROWS[CountOfCountries] = MAX(SUMMARIZED_ROWS[CountOfCountries]) ) )
This is going to give you the month where you have the most distinct countries in your data. You'll want to look at it in a card or similarly isolated visual as it is a measure and can be affected by filter context.
So, on the left is my mock data - 3 countries, 3 months each with a 1 month stagger. On the right you see the result of the Summarize table. Then the measure showing the final result.
Hope it helps.