I have a star schema model.
I have 2 tables, they look like this.
D_COURSES
Course_ID
Course_Name
Subject_Order
F_STUDENTCOURSES
COURSE_ID
POINTS_CAT_A
POINTS_CAT_B
I want to take the F_STUDENTCOURSES[COURSE_ID] value and look up the Subject_Order value in the D_COURSES table. If that value is 1, I want to sum the POINTS_CAT_A or POINTS_CAT_B value, for a divide function.
This is the DAX funciton I am using:
divide(
calculate(sum(F_STUDENTCOURSE[POINTS_CAT_A]),LOOKUPVALUE(D_COURSES[SUBJECT_ORDER],F_STUDENTCOURSE[COURSE_ID],1)),
calculate(sum(F_STUDENTCOURSE[POINTS_CAT_B]),LOOKUPVALUE(D_COURSES[SUBJECT_ORDER],F_STUDENTCOURSE[COURSE_ID],1)), "")
The error message I am getting is this:
Function LOOKUPVALUE expects a column reference as argument number 2.
What am I doing wrong?
Lookupvalue works this way lookupvalue(column to return, column to search in, value to search for from the column). In your case it would be lookupvalue(d[subject],d[course_id],f[course_id])....returns the d_subject by lookingup f_course_id values in d_course_id column.
I don't think Lookupvalue works inside calculate. Can you please try the following
MEASURE=
VAR _1 = Calculate(sum([catA]),Filter (values(d[sub_order]), d[sub_order]=1))
VAR _2 = Calculate(sum([catB]),Filter (values(d[sub_order]), d[sub_order]=1))
VAR _3 = DIVIDE (_1,_2)
RETURN _3
Assuming there is relationship it sums up catA and catB when suborder=1 and then divides
Related
I Have a TestTable that summarizes a table Receipts on the Month column and adds a column that counts the number of times (occurence) that each month appears in the Receipts Table.
TestTable = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
I want to add two columns to this TestTable which will tell me the following:
Sum the TotalReceiptsIssuedInThisMonth of the TestTable and show the
value in each row
For each Month (row), divide the, TotalReceiptsIssuedInThisMonth by the SumOfTotalReceiptsIssued
I know I can click "New Column" and use these formulas:
AvgPercentageReceiptsIssuedInThisMonth = TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued]
TotalReceiptsIssued = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
However, I need to integrate those two columns directly into the original TestTable formula to make it all happen in one step for use as a variable in the original Receipts table (otherwise I end up with circular logic if I try using relationships).
I've tried the following:
TestTable = SUMMARIZE(PPTs, PPTs[Month], "TotalReceiptsIssuedInThisMonth", SUM(PPTs[PPTs Issued]), "OccurenceOfMonth", COUNT(PPTs[Month]), "TotalReceiptsIssued", SUM(TestTable[TotalReceiptsIssuedInThisMonth]), "AvgPercentageReceiptsIssuedInThisMonth", TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued])
but this returns an error saying "A single value for column 'TotalReceiptsIssuedInThisMonth' in table 'TestTable" cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result." and I've tried:
TestTable =
VAR first = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
VAR second = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
VAR third = first[TotalReceiptsIssuedInThisMonth]/second
RETURN
third
But this returns an error saying "The variable'first' cannot be used in current context because a base table is expected."
So my question is, how do I go about combining these three steps into one DAX formula?
I would do something like this. I prefer ADDCOLLUMN(SUMMARIZE()...), because it helps to avoid a miscontexting. As you need a var table, then you need the CALCULATE in ADDCOLUMNS, as it adds the row context.
VAR TestTable =
ADDCOLUMNS(
SUMMARIZE(
Receipts
,Receipts[Month]
)
,"TotalReceiptsIssuedInThisMonth",CALCULATE(SUM(Receipts[Receipts Issued]))
,"OccurenceOfMonth", CALCULATE(COUNT(Receipts[Month]))
,"TotalReceiptsIssued ",SUM(Receipts[Receipts Issued])
)
RETURN
ADDCOLUMNS(
TestTable
,"AvgPercentageReceiptsIssuedInThisMonth"
,DIVIDE([TotalReceiptsIssuedInThisMonth],[TotalReceiptsIssued])
)
Check out More regarding the use of DAX to create a table
https://www.advancelearnlinux.com/how_to_create-table-in-microsoft-power-bi-using-dax-function/
Can someone explain how the first filter statement is different from second in DAX?
FILTER(Table, Table[Column] = "some text")
FILTER(VALUES(Table[Column]), (Table[Column] = "some text"))
The FILTER function is a table function, meaning it will return a table. In the case of your second example, it is likely that you will get a scalar value (a single value) because you are filtering a table (of one column of unique values) by a single value. In the first FILTER instance, however, you will be returning an entire table of the first argument, which has only been filtered by the conditional in the second argument. For reference, I have used the sample data built within the Power BI Desktop version to show you the key differences.
From your first FILTER example
FILTER( financials, financials[Country] = "Germany" )
Output
From your second FILTER example:
FILTER( VALUES( financials[Country] ), financials[Country] = "Germany" )
-- The extra parantheses around the second argument are not necessary...
Output
Therefore, the main functional difference is in the output. What are you wanting to return? An entire table or a specific value(s) of a column(s)?
I have a very simple attempt at a DAX measure:
CountAllCustomers = COUNTA( ALL( play[customer] ) )
What exactly does this part of the expression ALL( play[customer] ) return?
I assumed it returns a single column table and is therefore a valid argument for COUNTA - but it is not - why not?
How do I amend to keep ALL and count what is returned?
This expression does not work, because COUNTA expects a column reference (think column name), while ALL returns a table (table instance is not table reference, thus it's not valid parameter for COUNTA).
Maybe COUNTROWS is what you need in your case:
CountAllCustomers = COUNTROWS(ALL(play[customer]))
I have got this following SQL query that gives me the correct value from the database.
SELECT
SUM( DISTINCT_ORDER_NUMBERS )
FROM
(
SELECT STORE_KEY,
COUNT( DISTINCT TRANSACTION_NUM ) AS DISTINCT_ORDER_NUMBERS,
DATE_KEY,
TRANSACTION_TYPE_KEY
FROM Pos_Data
GROUP BY STORE_KEY,
DATE_KEY,
TRANSACTION_TYPE_KEY
)
AS A
I am however facing challenges writing a DAX formula for a measure in Power BI Here is what I have tried so far but I get an error.
Total Number Of Orders
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUM(_TotalOrders[DISTINCT_ORDER_NUMBERS])
Please assist
The SUM function expects a base table rather than a calculated table.
Try this instead:
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUMX(_TotalOrders, [DISTINCT_CHECK_SEQ])
Edit: If the difference you mentioned is related to nulls, then try this in place of DISTINCTCOUNT.
COUNTAX( DISTINCT( 'Pos_Data'[TRANSACTION_NUM] ), 'Pos_Data'[TRANSACTION_NUM] )
The COUNTAX function (as opposed to COUNTX) does not count nulls.
I tried using group by DAX function in PowerBI as Measure, New Column, New Table but I get an error in validating the function,
New Table = GROUPBY(
'tab1',
'tab1'[color],
"Group By color",
sum('tab1'[count_lables])
)
Error : Function 'GROUPBY' scalar expressions have to be Aggregation functions over CurrentGroup(). The expression of each Aggregation has to be either a constant or directly reference the columns in CurrentGroup().
The error says you need to use an aggregation function over a group, you are using SUM function which doesn't sum values over any group. In your case you need to use SUMX aggregation function with CURRENTGROUP() option. The CURRENTGROUP determines the groups that must be added based on the current row.
Try using the below expression instead.
New Table = GROUPBY (
tab1,
tab1[color],
"Group by color", SUMX ( CURRENTGROUP (), tab1[count lables] )
)
Let me know if this helps.