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)
)
Related
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.
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 table Like this,
Table1
I want to create a column called as Row Number using DAX and not Query Editor (M).
So the Expected output is,
This can be done in M - Power Query Side.
But, I am looking for a solution using DAX- Calculated Column.
Additional source data
The RANKX function should work fine for this purpose.
Row Number = RANKX ( Table1, Table1[ColA] )
Recommended reading:
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
I want to make a report in power bi, for which I need to find the latest record for all ids.
This is the SQL query that can be run on the table to achieve this:
SELECT * FROM placement
WHERE dateModified in (
SELECT max(dateModified)
FROM placement
GROUP BY ID
);
How can I achieve the same in Power BI using power query or DAX?
You can use the FILTER Function like this:
FILTER(placement, dateModified = MAX(placement[dateModified]))
not sure about the syntax, but if you provide some data we can check that is compiling.
I would probably use a report level filter on a calculated column:
IsTopDate = IF(table1[Date] = Calculate(Max(table1[Date]), All(table1)), 1, 0)
Then add top date as a report level filter on 1.
OR
Using PowerQuery, do something like so using Table.Column, List.Max and Table.SelectRows:
#"maxDate" = List.Max(Table.Column(#"Source" as table, "Date" as text) as list ),
#"Filtered Rows" = Table.SelectRows(#"Source", each [Date] = #"maxnum")
I'm new to DAX queries and trying to create a DAX query equivalent to following SQL query.
SELECT Year, Company, SUM(Actual Sales) as Actual, SUM(Planned Sales) as Planned
FROM Sales
GROUP BY Year, Company
I'm not able to take aggregate of multiple columns after groupby in DAX. How can I resolve this?
There are actually a few ways in Power BI to do this kind of aggregation. Here are three different options with slightly different results from each other.
Option 1: Query Editor
In the query editor window, select Transform -> Group By...
... and configure it as shown below (make sure to click the Advanced option).
This results in the below. Note - this replaces the starting table.
Option 2: Measures
In the regular window (not the query editor used in option 1), click on Modeling -> New Measure...
... to create two measures using the formulas below.
Actual = SUM(Sales[Actual Sales])
Planned = SUM(Sales[Planned Sales])
Once those measures are created, a visual (a matrix in this case) can be created with the desired headers and values.
Option 3: Columns
Similarly to option 2, click on Modeling -> New Column...
... to create two columns using the formulas below.
Actual = CALCULATE(
SUM(Sales[Actual Sales]),
FILTER(
Sales,
Sales[Year] = EARLIER(Sales[Year]) &&
Sales[Company] = EARLIER(Sales[Company])
)
)
Planned = CALCULATE(
SUM(Sales[Planned Sales]),
FILTER(
Sales,
Sales[Year] = EARLIER(Sales[Year]) &&
Sales[Company] = EARLIER(Sales[Company])
)
)
Now the aggregations are in columns that can be used report visuals.