Relational algebra: How can I add up values from rows and create a new column? - relational-algebra

Let say I have some relation R1:
How can I create a version of this relation with a new column, 'Total', which adds up the scores in R1 for each person, then displays the total in a new column? Such as this:
Problem: Here there is no 'Total' entry in the relation. I can't just use some conditional (σ) to select it. I have to somehow create it with a summation of the Scores from R1.
P.S. I am using Relax tool if this helps: https://dbis-uibk.github.io/relax/

Embarrassingly enough, there is a "SUM" function on the Relax tool, as described in some of their documentation:
http://dbis-uibk.github.io/relax/help.htm

Related

DAX Measure: True/False for selected column names

How to write measure that could filter Matrix based on True values in selected column names?
Example (download):
Estimated result: want to see only rows with Football = True
P.S. Field parameters are used to create slicer with column names
You'll need to fix this in the data model by introducing a Customer_Sport linking table with bi-directional cross-filtering to Customer.

Display related data when filtered in Power BI

Hello, here is my dataset :
What I would like to have is a filter on Campagne which shows all the other Campagne if the Contract number is the same. I explain myself. If I click on Campagne 3 in my filter, I want to see 1, 2, 3 and the attribution (the attribution by Campagne, not a Sum). Here is the expected result :
For now, the only solution that I have is to use a "temporary" table. But it's not optimal because I have to duplicate the data.
Any idea ?
Thanks a lot.
You are going to have to create an extra table or two to get the slicer to work how you want. There's no way around it, but you only need to duplicate part of the data. Here's what I would suggest.
Create a new Slicer table by summarizing the Campagne and Contrat columns from your original table.
Slicer = SUMMARIZE(Table1, Table1[Campagne], Table1[Contrat])
Now since you want the filtering to be done by Contrat and this would require a many-to-many relationship with the original table, you need to create a bridge table.
Contracts = VALUES(Table1[Contrat])
Now set up the relationships on Contrat making sure that the Slicer to Contracts relationship has cross-filtering enabled both ways.
Now you can use the Slicer[Campagne] column for your slicer and put Table1[Campagne] on your table and it should filter how you want.

DAX: Use measure outcome to populate calculated column without recalculating measure per row

I have two tables in PowerBI. One called 'Fact_WorstInstance' contains rows of (Index,Instance). For example:
1,2
2,1
3,2
One called 'Fact_AllInstances' contains rows of (Index,Instance,Value). For example:
1,1,'Red'
1,2,'Green'
2,1,'Amber'
2,2,'Red'
2,3,'Brown'
3,1,'Green'
3,2,'Blue'
The first table is essentially a pointer to the worst entry in the second table for the given index (as categorised by some external system).
There is a slicer on which Indexes are visible to the user.
What I want to do is find the worst instance value for the highest visible Index in the 'Fact_WorstInstance' table, and then get all the Index and Value rows from the 'Fact_AllInstances' table for that Instance.
For example, if the slicer isnt filtering then (3,2) should be the active row from from the 'Fact_WorstInstance' table and this should be used to get Instance 2 from the 'Fact_AllInstances' table
1,2,'Green'
2,2,'Red'
3,2,'Blue'
from the 'Fact_AllInstances' table.
I tried to do this in many different ways, by creating a measure on the 'Fact_WorstInstance' which gives the highest visible row. And then use this measure to create a calculated column on the 'Fact_AllInstances', with 1 for worst and 0 for not worst. And then use this calculated column as a filter in PowerBI.
The measure itself gives the expected value. The problem I have is when the measure is used to create the calculated column, I cannot find a way to stop the Index being filtered based on the row of calculated column - and therefore the measure outcome changes for each row.
My measure:
Worst Entry = CALCULATE(FIRSTNONBLANK(Fact_WorstInstance[Instance],1),filter(ALLSELECTED(Fact_WorstInstance),Fact_WorstInstance[Index]=MAX(Fact_WorstInstance[Index])))
My column:
WorstColumn = if(Fact_AllInstances[Instance]=[Worst Entry],1,0)
So instead of getting the output above, I get
1,2,'Green'
2,1,'Amber' --> because for Index 2, the measure gives index 1 as worst
3,2,'Blue'
This is a possible solution you might want to implement.
First of all, calculated columns are not affected by slicers/page filters, you will need to create a measure for that, so the way your are appraching the problem won't work.
Create an additional calculated table that holds unique instances values. In Power BI, Modeling tab there is a icon for creating a New Table, where you can use an expression to produce the table.
Use this expression:
IsntancesCalcTable = VALUES(Fact_WorstInstance[Instance])
Now you have a table called InstancesCalcTable in your model.
Drag the Instance column in the InstancesCalcTable and drop it in the Instance column of the Fact_WorstInstance, this will create a relationship between InstancesCalcTable and Fact_WorstInstance via Instance. A line between both tables will be drawn in the Relationships view, double click that line and you will see the Edit Relationship window.
Make sure it looks like this:
Then do the same for creating the relationship between InstancesCalcTable and Fact_AllInstances.
You will end with a model like this:
Then you can use Index column in the Fact_WorstInstance table, in a slicer and it will filter the Fact_AllInstances table to get only the instances selected.
However if you don't have any filter all rows in Fact_AllInstances will be shown.

Column not defined in current context

I'm trying something very simple in DAX:
[Balance] := 'Inventory'[Inventory Amount]*10
I get a "column does not exist in current context" error. What does this mean?
You are getting this error because it was expecting an aggregate. You are creating a calculated measure but referring to a row and it doesn't have context for this row. I'm not sure what you are trying to achieve. If you need to calculate things on a row level, you'll need to use a SUMX (or you need to create a calculated column that multiplies the Inventory Amount column by 10 and then SUM it. Otherwise, you can use SUM with just one calc.
If you just need to add all the inventory amounts on every row in the query context and then multiply by 10, you can do
Balance:= SUM('Inventory'[Inventory Amount])
If the number you multiple will not always be 10, I suggest putting that formula in a separate calculated column (let's call it 'Inventory[Factor]. Then create a calculation like:
Balance:= SUMX('Inventory', 'Inventory'[Inventory Amount] * 'Inventory'[Factor])
The difference between these two is that the SUM will do the aggregation first. The SUMX will do the row-level calculation first and then aggregate. Since I don't know what you are trying to do, I gave you both options. SUMX will probably be slower, so you probably want to avoid that.
Here is a relevant blog post on the subject. In general check out PowerPivotPro.com and DaxPatterns.com when trying to find your way through DAX.

PowerPivot DAX measure Filtering

I am starting to use all de PowerBI tools for a customer.
On the data they have three shops (1,2 and 4, named in the table "botiga") and diferent departments (named in the table "departament").
I need to calculate the sales (named "vendes" or Ventas[import]) of all the departments and all the shops but I have to exclude in the calculation the ones that correspond to the shop 1 and 2 and also correspond to the department 61.
All the realated columns are in the same table named "Ventas"
I did it and it Works with this measure:
vendes sense carnisseria:=[Vendes]-SUMX(FILTER(Ventas;Ventas[Botiga]<>"4" && Ventas[Departament]="61");Ventas[Import])
Vendes:=sum(Ventas[Import])
But I am sure that theri is a better way to do it, I have tried to do like in excel when we do SUMIF but doesn't seems to work.
I use FILTER beacouse I want that the data filters used in the associated pivot table remain.
Calculate is like a supercharged Sumif.
Try something like
Carnisseria:=
[vendes] - Calculate([vendes];Ventas[Botiga]<> "4"; Ventas[Departament]="61")
I would also check that you want any shop that isn't #4 or if you want just shops #1 and #2. While that yields the same results right now, it may not in the future.

Resources