How Crossjoin two tables in Dax created from Generateseries? - dax

I want to crossjoin to "table" created with GenererateSeries in a single Expression
Table =
VAR TableAnnee =
GENERATESERIES ( 2010; 2020; 1 )
VAR TableMois =
GENERATESERIES ( 1; 12; 1 )
RETURN
CROSSJOIN ( TableAnnee; TableMois )
Then I have an error message:
"CROSSJOIN function does not authorize two columns with the same name: [Value]."
How can I crossjoin TableAnnee and TableMois without creating two real tables but in one expression in that table?

You can use the SELECTCOLUMNS function to give your generated tables appropriate column names. The default column name is [Value], so you need to rename that default to do the crossjoin.
Table =
VAR TableAnnee =
SELECTCOLUMNS ( GENERATESERIES ( 2010; 2020 ); "Year"; [Value] )
VAR TableMois =
SELECTCOLUMNS ( GENERATESERIES ( 1; 12 ); "Month"; [Value] )
RETURN
CROSSJOIN ( TableAnnee; TableMois )

Related

How to count two column values

I want count two columns using power bi to create a visual.my measure as below
Test Measure = COUNTA('Export'[Line])+COUNTA('Export'[Line 2]),
You need to create a calculated table with all the lines values, like this
TableValues =
DISTINCT ( UNION ( DISTINCT ( 'Table'[Line] ), DISTINCT ( 'Table'[Line_2] ) ) )
With that table done, you can write the following measure
CountValues =
VAR _CurrentValue =
SELECTEDVALUE ( TableValues[Line] )
VAR _C1 =
CALCULATE ( COUNTA ( 'Table'[Line] ), 'Table'[Line] = _CurrentValue )
VAR _C2 =
CALCULATE ( COUNTA ( 'Table'[Line_2] ), 'Table'[Line_2] = _CurrentValue )
RETURN
_C1 + _C2
The calculations count all the instances of a specific Line. It's important that the TableValues doesn't have any relationship with the other tables.
Output
Using 'TableValues'[Line] as the Line and CountValues as the metric.

Problem in using filter context in CALCULATE

In PowerPivot DataModel I have written following DAX Code:
=VAR var1 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
ALL ( 'CapitalPrepayment'[EffectiveYear] )
)
VAR var2 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[EffectiveYear] = "1399",
'CapitalPrepayment'[Subject] = "Invoice"
)
VAR var3 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
'CapitalPrepayment'[EffectiveYear] = "1398"
)
VAR var5 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
ALL ( 'CapitalPrepayment'[Subject] )
)
RETURN
IF ( Var5 < 0, 0, IF ( Var5 = 0, 0, IF ( VAR1 + VAR2 <= 0, Var3, 0 ) ) )
and the result is:
As you can see, the problem is that grand total of [Var5] for some vendors and grand total for [Final] for all vendors is highly related to filters come from EffectiveYear ( at least Ithink so ), so the result would be zero. in addition, changing the filter context from visual ( for example removing [vendor] and [EffectiveYear] ) will disrupt the result. how can I fix this?
In my written DAX, [Var1]~[Var3] do include [subject] and [EffectiveYear] as filter arguments, but [Var5] does not for [EffectiveYear] . so I expect that the problem arise from [EffectiveYear].
I tried to include [EfectiveYear] in someway in CALCULATE but it didn't work.

How to get Closing Inventory Qty right - DAX

I can't find the right results for Closing Inventory, given the scenario below:
Measure added to Matrix Table:
Calculation=
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
SWITCH (
SELECTEDVALUE ( HelperTable[ID] ),
1, IF ( curDate = minDate, [OpenInventoryMeasure], BLANK () ),
2, IF ( curDate > minDate, [ProdMeasure], BLANK () ),
3, [QtySalesMeasure],
4, if (curDate > minDate,[Closing Inventory], BLANK())
)
Here are the measures created and used in the main calculation above:
OpenInventoryMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
return
CALCULATE(Sum('Production'[Qty]),FILTER('Calendar','Calendar'[Date] = minDate))
ProdMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
return
CALCULATE(Sum('Production'[Value]),FILTER('Calendar','Calendar'[Date] > minDate))
QtySalesMeasure = SUM(PedItens[QTDUND])
ClosingMeasure = [OpenInventoryMeasure ] + [ProdMeasure] - [QtySalesMeasure]
Main issue: Right now, Closing measure is not giving me the right result, although the measure that make it up are correct.
Tiny issue: Also, the order is not right in the table, although I had ordered it by ID.
Here's a screenshot of the resulting table:
I appreciate any help.
The problem is with the Opening Inventory measure, because when you do the CALCULATE you are now on the context of the Monday, so it's already filtered by Monday and then you're adding an additional filter of date = 7th which returns blank.
You need to clear the filters first and then apply the date filter:
OpenInventoryMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
return
CALCULATE(Sum('Production'[Qty]), ALLSELECTED('Calendar'), 'Calendar'[Date] = minDate)
Your Prod measure probably suffers from the same error, but I don't understand what's the logic behind it because you only filter based on min date and not on current date.

Count unique matching items with filter as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain three columns are Item, status, and filter.
The item contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The filter column contains two different filters which are A1 and A2.
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay". I am looking for count against filter A1 and A2 according to the comments.
I would like to create a new calculated column in the report table in order to get the unique count according to the comments and filter based on the data table columns item and status.
DATA:
REPORT
Alexis Olson helped the following calculated column in order to get the unique count. I am trying to add one more filter in existing DAX calculated column but it's not working. Can you please advise?
1.Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
2.Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
3.Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)
I think you can just add a filter to CALCULATE:
Filter by A1 Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment,
DATA[FILTER] = "A1"
)
For the second method,
Filter by A1 Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] && REPORT[FILTER] = "A1" ),
DATA[ITEM]
)
)
I do not recommend using the third one but it would be like this
Filter by A1 Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF (
CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS]
&& CALCULATE ( SELECTEDVALUE ( DATA[FILTER] ) ) = "A1",
1,
0
)
)

SQL ERROR When running query

I've been trying to execute below code, but it's not running. SQLFiddle gives
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.'EO' AS 'EO','EF'.'AC' AS 'AC','EO'' at line 2
I don't know what I'm doing wrong.
SELECT DISTINCT
'EO'.'EO' AS 'EO',
'EF'.'AC' AS 'AC',
'EO'.'WO' AS 'WO',
'EO'.'WOF' AS 'WOF',
'EF'.'PN_SN' AS 'PN_SN',
'EC'.'CD' AS 'CD',
'EF'.'PN' AS 'PN',
'EO'.'EOC' AS 'EOC',
'EO'.'STAT' AS 'STAT',
'EF'.'OVRD' AS 'OVRD',
'EF'.'SEL' AS 'SEL',
'EF'.'ED' AS 'ED',
'EF'.'SCHED' AS 'SCHED',
'EF'.'SAD' AS 'SAD',
'EF'.'SA' AS 'SA',
'EO'.'EOD' AS 'EOD'
FROM
EO,
EC,
EF
WHERE
(
'EO'.'EO' = 'EC'.'EO'
)
AND
(
'EF'.'PSN' = 'EC'.'PSN'
)
AND
(
'EO'.'WB' = 'Y'
)
AND
(
'EC'.'CONT' = 'W & B'
)
;
SELECT
'EC'.'RSTD' AS 'RSTD'
FROM EC RIGHT JOIN EF
ON ( 'EF'.'EO' = 'EC'.'EO' )
ON ( 'EF'.'AC' = 'EC'.'AC' )
ON ( 'EF'.'PN' = 'EC'.'PN' )
ON ( 'EF'.'PSN' = 'EC'.'PSN' )
Single quotes - ' - are for literals. Use double-quotes - " - for identifiers ...
SELECT
"EC"."RSTD"
FROM EC RIGHT JOIN EF
ON ( "EF"."EO" = "EC"."EO" )
ON ( "EF"."AC" = "EC"."AC" )
ON ( "EF"."PN" = "EC"."PN" )
ON ( "EF"."PSN' = "EC"."PSN" )
... or, even better, omit them altogether:
SELECT
EC.RSTD
FROM EC RIGHT JOIN EF
ON ( EF.EO = EC.EO )
ON ( EF.AC = EC.AC )
ON ( EF.PN = EC.PN )
ON ( EF.PSN = EC.PSN )
Obviously keep the single quotes when appropriate e.g. comparing a column to a literal:
where EO.WB = 'Y'
Also note that it is unnecessary to use a column alias when you're not changing the column name: select ec.rstd as rstd is redundant, and indeed annoying. Reserve their use for renaming columns e.g. select ec.rstd as my_new_col_name

Resources