PowerBi: Returning id in DAX formula - powerbi

I am a new PowerBi user and I would like to create a primary/foreign key relationship using a DAX formula which will return the id of the corresponding line to me.
I have two tables:
CompanyXFlights
GlobalFlights
The dax formula:
Exist =
COALESCE (
CALCULATE (
COUNTROWS ( CompanyXFlights );
FILTER (
CompanyXFlights;
OR (
AND (
GlobalFlights[ArrivalDateTime]
< CompanyXFlights[DateTime] + TIME ( 0; 10; 0 );
GlobalFlights[ArrivalDateTime]
> CompanyXFlights[DateTime] - TIME ( 0; 10; 0 )
);
AND (
GlobalFlights[DepratureDateTime]
< CompanyXFlights[DateTime] + TIME ( 0; 10; 0 );
GlobalFlights[DepratureDateTime]
> CompanyXFlights[DateTime] - TIME ( 0; 10; 0 )
)
)
&& GlobalFlights[AircraftRegistrationCode]
= CompanyXFlights[AircraftRegistrationCode]
)
);
0
)
I'm using this formula to get the number of corresponding lines, but now I want it to return the index (id) of the line. What should I change? Should I put CompanyXFlights[Id] instead of COUNTROWS(CompanyXFlights) or should I use another formula?

I found the solution, and it was quite simple, i just changed COUNTROWS(CompanyXFlights) by SELECTEDVALUE(CompanyXFlights[Id]);

Related

Create measure to calculate rows based on TO and FROM figures in another table

I have a report that fetches weekly data. I have a weeks table that contains the weeks and dates and relationships set up with the below tables.
I have a QueueSummary table;
I have ConciergeHours table;
The concierge hours differ from day to day, so i keep the start and finish time in a table so we can update it dynamically.
I am trying to create a measure to calculate how many interactions we have had, between the hours in the ConciergeHours table. Keep in mind this is a weekly report, so on any given day during the week, the hours may be different.
The calculation we are after is;
Number interactions accepted on queue LinkConciergeVQ (QueueSummary table) between the starthour and endhour in ConciergeHours table MINUS Number interactions offered on queue LinkVQ (QueueSummary table) between the starthour and endhour in ConciergeHours table
I currently have the measure looking like this. It works, but the between hours are not respected correctly, which i understand would be a result of the MIN, MAX.
m_deflections =
CALCULATE (
SUM ( QueueSummary[s_accepted] ),
QueueSummary[queue] = "LinkConciergeVQ",
filter(QueueSummary, [hour] >= min(ConciergeHours[starthour])),
filter(QueueSummary, [hour] < max(ConciergeHours[endhour]))
)
- CALCULATE (
SUM ( QueueSummary[s_offered] ),
QueueSummary[queue] = "LinkVQ",
filter(QueueSummary, [hour] >= min(ConciergeHours[starthour])),
filter(QueueSummary, [hour] < max(ConciergeHours[endhour]))
)
I hope i've explained this correctly.
If anyone could offer any help!?
Thanks so much
Not sure I understand your table relationships, but if its 1:* from ConciergeHours to QueueSummary via date then I think you can use this measure, or at least it puts you on the right track, I hope:
m_deflections =
var s_accept =
SUMX(
'ConciergeHours',
var startH = 'ConciergeHours'[startHour]
var endH = 'ConciergeHours'[endHour]
return
CALCULATE(
SUM('QueueSummary'[s_accepted]),
'QueueSummary'[hour] >= startH && 'QueueSummary'[hour] < endH
)
)
var s_offered =
SUMX(
'ConciergeHours',
var startH = 'ConciergeHours'[startHour]
var endH = 'ConciergeHours'[endHour]
return
CALCULATE(
SUM('QueueSummary'[s_offered]),
'QueueSummary'[hour] >= startH && 'QueueSummary'[hour] < endH
)
)
return
s_accepted - s_offered
I would collapse the 2 pairs of FILTER functions into a single FILTER for each CALCULATE. That will apply the filters against starthour and endhour in combination (the && means AND), where I expect they are currently applying as OR logic so not having any effect.
Along the lines of this:
m_deflections =
CALCULATE (
SUM ( QueueSummary[s_accepted] ),
QueueSummary[queue] = "LinkConciergeVQ",
FILTER (
QueueSummary,
[hour] >= MIN ( ConciergeHours[starthour] )
&& [hour] < MAX ( ConciergeHours[endhour] )
)
)
- CALCULATE (
SUM ( QueueSummary[s_offered] ),
QueueSummary[queue] = "LinkVQ",
FILTER (
QueueSummary,
[hour] >= MIN ( ConciergeHours[starthour] )
&& [hour] < MAX ( ConciergeHours[endhour] )
)
)

Exclude specific calculation from subtotal and grand total lines

I have this table :
And I want to calculate for each day and for each group 1and2 % which is
(Attribute1/Attribute2)*100
But I don't want to include 1and2 % in Group Subtotal neither on total
I created a new table :
Table3 = UNION(DISTINCT('Table'[Attributes]);{{"1and2 %"}})
And i made this mesure to calculate 1and2 %:
Measure =
SUMX (
DISTINCT ( 'Table3'[Attributes] );
SWITCH (
'Table3'[Attributes];
"1and2 %"; DIVIDE (
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute1" );
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute2" );
0
) * 100;
VAR a = 'Table3'[Attributes]
RETURN
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = a )
)
)
And this is the result I'm getting :
I want to exclude 1and2 % (and every Attribute containing % in the future) from group subtotal and the total.
I'm really new to PowerBI and not really familiar with Excel-like formulas.
Can anyone please help me up with that?
Regards
Someone from PowerBI forum gave me this solution and it's exactly what im looking for:
Create another mesure using the existing :
Measure 2 =
VAR __Table =
ADDCOLUMNS(
SUMMARIZE(
'Table 3',
[Group],
[Attribute],
"__Measure",[Measure]
),
"__IncludeInTotals",SEARCH("%",[Attribute],,-1)
)
RETURN
IF(
HASONEVALUE('Table 3'[Attribute]),
[Measure],
SUMX(FILTER(__Table,[__IncludeInTotals] = -1),[__Measure])
)
I think you can get what you want if you only calculate the 1and2 % part when that's the only thing in the filter context and do a normal sum otherwise.
Measure =
IF (
SELECTEDVALUE ( 'Table3'[Attributes] ) = "1and2 %";
DIVIDE (
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute1" );
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute2" );
0
) * 100;
SUM ( 'Table'[Value] )
)
For the subtotals and grand total, SELECTEDVALUE returns blank so the condition fails and you just get the sum.

I will like a script in power bi that will calculate percentage increase or decrease from one month to the previous month

I want to display percentage increase or decrease in total for each month as I select each month i.e when I click on FEB, it should tell me whether there was a percentage increase/decrease in expenses compared to JAN.
I have tried different codes but keep getting an error message.
Here is a DAX CODE I tried:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
population[MONTH]
= ( EARLIER ( population[MONTH] ) - 1 )
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH ),
0,
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH
I want a new column created to display the percentage increase or decrease from a month to its previous month.
Here is a screenshot of the excel document:
The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.
Now you need to change the script to work with DateDiff:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
DATEDIFF(population[MONTH], EARLIER ( population[MONTH] ),MONTH) = 1
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH );
0;
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH)

Calculate Column Sum of a Generated Table - DAX

I've been trying to get the sum of the following table's column VALUE using DaxStudio in order to put that on PowerBI once is's ok, since PBI can get slow if you test a code for large calculated tables.
Table from DaxStudio
BU VALUE
------------------------
FOODS 0.0000
FIBI 0.0000
GEOS/CIS 0.7300
CASC
S_S
SGS
COCOA
COCOA/SSSA
CORPORATE
N/A
CIS
The code behind it:
DEFINE
VAR TOTAL =
CALCULATE (
SUMX (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR]
);
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
)
)
EVALUATE
SUMMARIZE (
CALCULATETABLE (
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> 0
)
);
PACKAGING_POWERBI_YEARLY_2[BU];
"VALUE"; FORMAT (
(
CALCULATE (
SUMX (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR]
);
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
)
) / TOTAL
)
* (
CALCULATE (
SUMX ( PACKAGING_POWERBI_YEARLY_2; PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] );
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
)
)
);
"0.0000"
)
)
Instead of generating the table I would like to sum the results of column VALUE, but the only way I got to plot a result without error is through a "CALCULATED TABLE" (above).
Every filter I try leads to an error.
The idea is to get just the sum of that calculated column VALUE:
0.7300
But every simpler filter or SUMX condition I try, an error pops-up.
I'd think you could clean it up to look something like this:
CALCULATE (
SUMX (
VALUES ( PACKAGING_POWERBI_YEARLY_2[BU] ),
SUM ( PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR] )
/ CALCULATE (
SUM ( PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR] ),
ALL ( PACKAGING_POWERBI_YEARLY_2[BU] )
)
* SUM ( PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] )
),
FILTER (
PACKAGING_POWERBI_YEARLY_2,
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> 0
)
)
This uses SUMX to iterate through each of the BU values and for each one, calculates the value
(QUANTIDADE_ANTERIOR / Total QUANTIDADE_ANTERIOR ) * PRECO_PONDERADO
where the filters are reused instead of repeated.
I can't guarantee that this code works exactly as intended, but it should point you in a better direction.

filtering measures based on two columns in power bi dax

I want to use a measure and filter the result based on the columns:
My measure is :
TotalProductionCon =
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] )
and I want it to summarize only when column année = column year.
I tried CALCULATE and FILTER;
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] );
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
but it generates an error that the columns contain much value and I need to use aggregation.
Can you help me?
The problem with your formula is that you limited ALL function to only one column (Annee), and as a result FILTER does not "see" the other column it needs.
To fix that, change your formula as follows:
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL (
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année];
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
);
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
I am assuming here that your choice of ALL function is appropriate; otherwise you might need to use a different technique such as SUMMARIZE function.