I have two tables in Power BI model
Table A
value1
value2
value3
....
value 1000
Table B
value 1 | 10
value 2 | 10
value 1 | 50
value 3 | 10
value 1 | -10
value 2 | 70
Can I make a new column (or measure) in Table A to Sum UP connected values ???
Expected RESULT:
value 1 | 50 --- (10+50-10)
value 2 | 80 --- (10+70)
value 3 | 10 --- (10)
Just something like SUM.IF in Excel, which can I drag to all rows ? Thanks in advance.
I tried to CALCULATE, but I can't do this for all different rows in Table A
You don't need Table A for this. SUMMARIZE() will create a column of distinct values to group by. Use the following Calculated Table. Note that this is NOT a MEASURE!
Result =
SUMMARIZE(
'Table B',
'Table B'[value ID],
"Sum", SUM('Table B'[number])
)
Yes, Possible ! Just Add a new column on your table, and write this DAX Code after relationship is created!
Related
I have table 'tblA' with only 1 column named 'Value'
Value
1
2
The second table 'tblB' with several columns
Col1 Col2
Test A
Dump B
How can I have a join between them so that I will have new table with result like this (each value in tblA will fill in to all rows in tblB):
Col1 Col2 Value
Test A 1
Dump B 1
Test A 2
Dump B 2
I also tried to use for loop to get one-by-one value in tblA. But it seems that DAX didn't support loop.
Please advise.
Use expression for a calculated table
tblC = CROSSJOIN ( tblA, tblB )
I have TABLE A
In that table I have a measure with values like so:
Targets|
--------
4 |
5 |
6 |
In the same table I have a calculated column (summed totals) like so:
Totals |
--------
10 |
11 |
12 |
Because this is a direct query data source, query editor is disabled and manipulation must be done through DAX formulas.
I would like to do a simple operation of Targets-Totals
Code I've tried for a calculated column:
test = TableA[targets] - TableA[totals]
However this results in an error:
The column TableA[test] cannot be pushed to the remote data source and cannot be used in this scenario.
How can I create a new column with the above operation considering the fact that one column is a 'measure ' and the other a 'calculated column'
In this case, you will need a measure that does a row by row calculation, but not as a calculated column. For this you will need SUMX which will do a iteration.
Your measure should be:
New Measure = SUMX(TableA, [targets] - [totals])
I have been working on this Power BI Report and would like some assistance with a slicer used for a matrix.
I need the slicers "MB Data Used", "Calls Made", and "SMS Sent" to slice the Matrix Grand Total fields (on the far right) instead of the value fields. My current slicers work great on the value fields.
Data is filled by a table:
----------------------------------------------------
|ph_id | month | data_used | calls_made | sms_sent |
| 1 | 1/1/19| 123 | 0 | 33 |
| 2 | 1/1/19| 87 | 22 | 0 |
| 3 | 1/1/19| 0 | 0 | 0 |
| 1 | 1/2/19| 0 | 55 | 33 |
| 2 | 1/2/19| 87 | 22 | 77 |
| 3 | 1/2/19| 0 | 0 | 0 |
----------------------------------------------------
Which links to a few others to get related data.
My goal is to be able to see which phone numbers have had no data/call/sms use over the last X months instead of just filtering the ones which contain a 0. In this scenario, when the slicers are all set to 0 and the date range is set 1/1/19-1/2/19, only ph_id 3 should show.
Edit:
W.B. - see this image
You need to use another, unrelated table for your slicers. The best way to create such table is to use the what-if parameter option in the modelling tab (assuming you have any recent version of PBI Desktop).
Or, if you want to base the slicer on number of calls from actual data, you would create the slicer table using New table option and the following formula: CallSlicer = GENERATESERIES(MIN(Data[calls_made]), MAX(Data[calls_made]), 1). The one at the end indicates the step, so you can adjust it, if you want your users to use the slicer in, for instance, increments of 10 or 20.
Now, when you use the generated CallSlicer column, which looks like this:
You will be able to filter your results like this: Your filtered measure = CALCULATE([your_measure], FILTER(Data, Data[calls_made] >= MIN(CallSlicer[CallSlicer]) && Data[calls_made] <= MAX(CallSlicer[CallSlicer]))). You then use your filtered measure in the matrix visual.
EDIT:
Here's a working sample: https://1drv.ms/u/s!AmqvMyRqhrBpgtRGGbJ6w-b66uBENQ?e=67JduS
I've updated the sample - now it shows 2 scenarios. One table reacts to the slicer at individual cell level, the other one at the grand total level.
The key to have the first table working is shown above, below is a solution for the second table, that filters rows at the grand total level:
Create a measure that will show sum for all dates/months, as an example:
CallSumTotal =
VAR tab =
FILTER (
CALCULATETABLE (
SUMMARIZE ( Data, Data[id], "calls_made", SUM ( Data[calls_made] ) ),
ALLSELECTED ( Data[month] )
),
[calls_made] >= MIN ( CallSlicer[Value] )
&& [calls_made] <= MAX ( CallSlicer[Value] )
)
RETURN
SUMX ( tab, [calls_made] )
Now in the matrix use a regular sum measure, but create a visual level filter for CallSumTotal and set it to is not blank
assume I have a db table with following rows:
some_key - price
1 | 100
1 | 100
2 | 150
2 | 150
3 | 100
3 | 100
I want to list users with their total expending according to orders table. but for whatever (stupid) reason each row may have been duplicated. so I should add distinct on "some_key" column and obviously this code bellow won't work.
how could I annotate Sum of prices with distinct on "some_key" column
query = User.objects.filter(<...>).annotate(price_sum=Sum("orders_set__price", distinct=True))
any suggestion?
I currently have a bar graph in power-bi. The graph has distinct id's on the x axis and the amount of mail they received on the y axis. In the Table I am using, I have SalesYear, id, and a Val column which has the value 1 in each row. The id shows up multiple times in the table, sometimes more than twice in the same year.
The problem is I want the graph reversed. I would like to bucket people based on how much mail they received. Then use a slicer to see how much they receive per year. I have been struggling to find a solution on my own, would anyone have any ideas on how to approach this.
Table l looks like this:
id | salesYear | Val
10 | 2012 | 1
11 | 2012 | 1
11 | 2013 | 1
10 | 2012 | 1
10 | 2013 | 1
12 | 2012 | 1
12 | 2012 | 1
So in the visualization I want to show that on the x-axis that people who received 1 piece of mail = 0, 2 pieces of mail = 2, 3 pieces of mail = 1. My question is how can i achieve this will a Stacked Column chart. Any suggestions would be greatly appreciated!
15k | Y axis would be amount of people who recieved 1 piece, 2 piece, etc..
14k | _
13k | | |
12k | _| |
11k || | |
10k ||_|_|_________________________
1 2 3 4 5 6 7 8 9 <-AmountOfMailRecieved
1) Starting with your sample data in Power BI.
2) Create a new calculated table that is a distinct list of years from your source table.
Years = DISTINCT(
SELECTCOLUMNS(
Mail,
"SalesYear", Mail[salesYear]
)
)
3) Create another calculated table that crossjoins the year table with a series of integers. Power BI might indicate the [Value] has an error (like it does for me in the picture below), but it works properly.
MailCounts = SELECTCOLUMNS(
CROSSJOIN(Years, GENERATESERIES(1, 20)),
"SalesYear", Years[SalesYear],
"MailReceived", [Value]
)
4) Add a calculated column that counts the number of IDs in the source table that match the year and mail count. For example, the first row is counting how many distinct IDs show up exactly once in the source table for the year 2012; it's 1 because only ID 11 shows up in 2012 once.
PersonCount = CALCULATE(
DISTINCTCOUNT(Mail[id]),
FILTER(Mail,
Mail[salesYear] = EARLIER(MailCounts[SalesYear]) &&
EARLIER(MailCounts[MailReceived]) = CALCULATE(
COUNTROWS(Mail),
FILTER(Mail,
Mail[salesYear] = EARLIER(Mail[salesYear]) &&
Mail[id] = EARLIER(Mail[id])
)
)
)
)
5) Create relationships between your source table and the year table, and then between the year table and the count table. This will allow the creation of a slicer based on the year of your source table to filter the results from the count table.
6) Optionally, you can hide the year field in the source and count tables. After doing that if you desire, create a chart as configured in the picture below
7) Create a slicer from the year table as shown in the picture below.
And that's it. The chart should match with your expected outcome and can be filtered by year.
You'll be using DISTINCTCOUNT either in a measure or a column.
VariableName = DISTINCTCOUNT([AmountofMailReceived])