DAX: Avoid summing up repeated numbers - powerpivot

I have a table that includes columns for accountID, revenue, quarter and estimated revenue. the issue with this table is that the revenue repeats for each quarter.
The table is perfect if I'm trying to find the estimate, but as soon as I sum the revenue, it basically quadruples for each account.
I can divide by 4, but that gives a wrong revenue number for each quarter.
Is there a DAX function that allows me to show "4000" for each quarter, but at account/company level, it would not quadruple in size?
AccountID | Revenue | Quarter | Estimate
123 | 4000 |Q1 | 4000
123 | 4000 |Q2 | 5000
123 | 4000 |Q3 | 2000
123 | 4000 |Q4 | 4000
456 | 3000 |Q1 | 4000
456 | 3000 |Q2 | 5000
456 | 3000 |Q3 | 1000
456 | 3000 |Q4 | 3000
What I would like to see in pivot
Account ID | Quarter | Sum of Revenue | Sum of Estimate
123 | Q1 |4000 | 4000
123 | Q2 |4000 | 5000
123 | Q3 |4000 | 2000
123 | Q4 |4000 | 4000
123 Total |4000 | 15000
456 | Q1 |3000 | 4000
456 | Q2 |3000 | 5000
456 | Q3 |3000 | 1000
456 | Q4 |3000 | 3000
456 Total |3000 | 13000
Grand Total |7000 | 2800

Create two measures EstimateMeasure and RevenueMeasure:
EstimateMeasure = SUM(Table1[Estimate])
RevenueMeasure = SUMX(VALUES(Table3[Revenue]),Table3[Revenue])
Add previously created measures to Values and AccountID & Quarter in your pivot table.
It should produce the following output in Power BI.
Note I don't have access to PowerPivot in this moment, so I can't post the complete table with subtotals. But you can add subtotals in PowerPivot to get the total per AccountID.
Let me know if this helps.

Related

SQL Query for a Compare Report from single table

SQL Newb here, I'm having a bit of trouble understanding this problem. How can I write a single SELECT statement where I can have columns with their own WHERE clauses, do a calculation, and group the results.
I can write the query to sum totals and do averages checks grouping by revenue center and fiscal year, but I can't quite grasp how to do side by side compare with a single query.
SALES DATA
| RevenueCenter | FiscalYear | TotalSales | NumChecks |
|---------------|------------|------------|-----------|
| market | 2019 | 2000.00 | 10 |
| restaurant | 2019 | 5000.00 | 25 |
| restaurant | 2020 | 4000.00 | 20 |
| market | 2020 | 3000.00 | 10 |
COMPARE REPORT
| RevenueCenter | TotalSales2020 | TotalSales2019 | %Change | AvgCheck2020 | AvgCheck2019 | %Change |
| market | 3000.00 | 2000.00 | +50% | 300.00 | 200.00 | +50% |
| restaurant | 4000.00 | 5000.00 | -20% | 200.00 | 200.00 | 0% |
Would this help? No big deal, just a self-join with some arithmetic.
SQL> with sales (revenuecenter, fiscalyear, totalsales, numchecks) as
2 -- sample data
3 (select 'market' , 2019, 2000, 10 from dual union all
4 select 'market' , 2020, 3000, 10 from dual union all
5 select 'restaurant', 2019, 5000, 25 from dual union all
6 select 'restaurant', 2020, 4000, 20 from dual
7 )
8 -- query you need
9 select a.revenuecenter,
10 b.totalsales totalsales2020,
11 a.totalsales totalsales2019,
12 --
13 (b.totalsales/a.totalsales) * 100 - 100 "%change totalsal",
14 --
15 b.totalsales / b.numchecks avgcheck2020,
16 a.totalsales / a.numchecks avgcheck2019,
17 --
18 (b.totalsales / b.numchecks) /
19 (a.totalsales / a.numchecks) * 100 - 100 "%change numcheck"
20 from sales a join sales b on a.revenuecenter = b.revenuecenter
21 and a.fiscalyear < b.fiscalyear;
REVENUECEN TOTALSALES2020 TOTALSALES2019 %change totalsal AVGCHECK2020 AVGCHECK2019 %change numcheck
---------- -------------- -------------- ---------------- ------------ ------------ ----------------
market 3000 2000 50 300 200 50
restaurant 4000 5000 -20 200 200 0
SQL>

Calculation of interest on balance due

I have 3 tables Invoice, Payments and InterestRate. For purposes of simplicity I am representing each of the tables as follows.
The Invoice table has the following columns (the InvNum is the primary key)
+--------------+-------------+---------------+
| InvNum | InvDate | InvAmt |
+--------------+-------------+---------------+
| 123 | 2017-03-09| 15000 |
| 456 | 2017-01-13| 20000 |
+--------------+-------------+---------------+
The Payment table is as follows (the InvNum in the payments table is a foreign key which links to the Invoice table above
+--------------+------------+----------+
| payment_date | InvNum | amount |
+--------------+------------+----------+
| 2017-08-10 | 123 | 5000 |
| 2017-08-15 | 456 | 3000 |
| 2017-09-15 | 123 | 3000 |
| 2017-10-11 | 456 | 4000 |
| 2017-10-16 | 123 | 3500 |
| 2017-11-10 | 123 | 3000 |
| 2017-11-15 | 456 | 2500 |
+--------------+------------+----------+
My InterestRate looks something like this
+--------------+-------+
| ResetDate | IntRate|
+--------------+--------+
| 2016-01-01| 10 |
| 2017-06-08| 10.25 |
| 2017-11-03| 10.67 |
| 2018-05-03| 10.83 |
| 2018-07-29| 10.72 |
| 2019-01-01| 10.67 |
+------------+----------+
What I need is to calculate the interest for each Invoice after considering each payment and the final balance due on the invoice (in case it has not been fully paid). The final table should look something like this
InvNum IntFrom IntTo Amount IntRate Interest Payment Balance
123 2017-03-09 2017-06-07 15000 10 xxx 0 15000
123 2017-06-08 2017-08-10 15000 10.25 yyy 5000 10000
123 2017-08-11 2017-09-15 10000 10.25 zzz 3000 7000
123 2017-09-16 2017-10-16 7000 10.25 aaa 3500 3500
123 2017-10-17 2017-11-02 3500 10.67 bbb 0 3500
123 2017-11-03 2017-11-15 3500 10.67 ccc 2500 1000
123 2017-11-16 2018-05-02 1000 10.67 ddd 0 1000
123 2018-05-03 2018-07-28 1000 10.83 eee 0 1000
123 2018-07-29 2018-12-31 1000 10.72 fff 0 1000
123 2019-01-01 (today) 1000 10.67 ggg 0 1000
I have not shown the results of Inv#456. But the desired solution needs to group each of the invoices in the invoice table.
So in effect the interest calculation takes into account the payments made against each invoice (listed in the Payments table) as well as the changing interest rates (listed in the InterestRate table). Of equal importance is the fact that the interest on the balance due on each invoice (1000 for inv#123) needs to calculated till the date of running the query.
One of the challenges I am also facing is how can I capture the above results in a table. Would it mean dropping and recreating the table every time you run the query (since everytime the date changes the interest amount on the balance due will undergo a change). So unless only the interest on the balance per invoice can be updated, it would (should?) necessarily mean that the table has to be dropped and re-created. Alternatively, the results have to be viewed through a view query.
I hope I have been able to articulate my problem satisfactorily.
Thanks a lot for your time and attention to helping me out

Comparing SUM of values with different tables in SQL Server

I have two tables holding similar values, and I need to compare the two and find the differences between them:
SQL FIDDLE - http://sqlfiddle.com/#!6/7412e/9
Now you can see there is a difference between between the 2 tables for the figures in Jun-17.
AS you can see (as a total for everyone) table 1 has £75 for June but table 2 has £125 for june.
The result I'm looking for is when amounts are summed together and compared between tables on a monthly basis, if there is a difference in amount between the two tables I want it listed under 'Unknown'.
| MonthYear | Person | Amount | Month total
+-----------+--------+--------+--------------
| Jun-17 | Sam | 25 | 75(Table1)
| Sep-17 | Ben | 50 | 50(Table2)
| Jun-17 | Tom | 50 | 75(Table1)
| Jun-17 | Sam | 25 | 125(Table2)
| Sep-17 | Ben | 50 | 50(Table2)
| Jun-17 | Tom | 50 | 125(Table2)
| Jun-17 | | 50 | 125(Table2)
Now when there is a difference between the amount total over a month I want the difference to be classed as unknown
e.g
| MonthYear | Person | Amount | Month total
+-----------+--------+--------+--------------
| Jun-17 | Sam | 25 | 75(Table1)
| Sep-17 | Ben | 50 | 50(Table2)
| Jun-17 | Tom | 50 | 75(Table1)
| Jun-17 | Sam | 25 | 125(Table2)
| Sep-17 | Ben | 50 | 50(Table2)
| Jun-17 | Tom | 50 | 125(Table2)
| Jun-17 | Unknown| 50 | 125(Table2)
I understand that you could create a case when the person is null to display unknown but i need it to be specifically calculated on the difference between the 2 tables on a monthly calculation.
Does this make sense to anyone, its really hard to explain.
Generally, in any FROM clause a table name can be replaced with another SELECT as long as you give it a corelation name (t1 and t2 in this one):
SELECT t1.MonthYear, t1.AmountT1, t2.AmountT2, t1.amountT1 - isnull(t2.amountT2, 0) as Unknown'
from
( SELECT
MonthYear,
SUM(Amount) AS [AmountT1]
FROM
Invoice
GROUP BY MonthYear) t1
left outer join
( SELECT
MonthYear,
SUM(Amount) AS [AmountT2]
FROM
Invoice2
GROUP BY MonthYear) t2 on t2.MonthYear = t1.MonthYear

SQL Server: subtract or sum between rows in the same column then update the results

I have a table my MS SQL Server, which has a standard row type BID. I want to subtract all the row type ASK from BID in column availableAmount then update the result into the BID row at the same column. At the same time, I want to calculate the sum of fulfilledAmount of row type ASK then update the result into the same column of row type BID. I'm still learning SQL but is it possible to do this complicate process?
EDIT
Example:
Before execution:
id | type | availableAmount | fulfilledAmount |
-----+------+-----------------+-----------------+
abcv | ASK | 500 | 500 |
xyzs | ASK | 600 | 600 |
scwd | BID | 10000 | 0 |
cd21 | ASK | 1300 | 1300 |
sadc | ASK | 3400 | 3400 |
2w3e | ASK | 2500 | 2500 |
After execution:
id | type | availableAmount | fulfilledAmount |
-----+------+-----------------+-----------------+
abcv | ASK | 500 | 500 |
xyzs | ASK | 600 | 600 |
scwd | BID | 1700 | 8300 |
cd21 | ASK | 1300 | 1300 |
sadc | ASK | 3400 | 3400 |
2w3e | ASK | 2500 | 2500 |
If I understood correct, you need something in the line of:
UPDATE myTable
SET availableAmount = availableAmount - (
SELECT SUM(availableAmount)
FROM myTable
WHERE TYPE = 'ASK'
)
,fulfilledAmount = (
SELECT SUM(fulfilledAmount)
FROM myTable
WHERE TYPE = 'ASK'
)
WHERE TYPE = 'BID'
AND ID ='scwd'

Select and group by - calculated field

I'm having some issues to complete a SQL statement in SQL Server 2008.
My 'query1' is the following:
SELECT [Vc_MONTH],
[Vc_STATE],
[Vc_PRODUCT],
SUM ([TOTAL]) as Total_Units,
SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY [Vc_MONTH],
[Vc_REGION],
[Vc_PRODUCT],
This query selects Month, Region, Product, Sum of Total Units and Sum of Open Units.
I already group by Month, Region and Product. (I have plenty more lines)
This query works.
What I need is another 'query2' that groups by (ALL) the months listed on the table and then an union of this two selects.
At the end I need something like this
query1
|MONTH | STATE | PRODUCT | TOTAL | OPEN |
|:-----|:------|:--------|:------|:-----|
|JAN | CA | PENCIL | 200 | 160 |
|JAN | FL | BOOK | 300 | 280 |
|FEB | CA | PENCIL | 180 | 150 |
|FEB | FL | PENCIL | 250 | 100 |
|MAR | CA | BOOK | 250 | 100 |
|MAR | FL | BOOK | 100 | 50 |
query2 - This is what I need
|MONTH | STATE | PRODUCT | TOTAL | OPEN |
|:-----|:------|:--------|:------|:-----|
|JAN | CA | PENCIL | 200 | 160 |
|JAN | FL | BOOK | 300 | 280 |
|FEB | CA | PENCIL | 180 | 150 |
|FEB | FL | PENCIL | 250 | 100 |
|MAR | CA | BOOK | 250 | 100 |
|MAR | FL | BOOK | 100 | 50 |
UNION
|ALL | CA | PENCIL | 380 | 310 |
|ALL | CA | BOOK | 250 | 100 |
|ALL | FL | PENCIL | 250 | 100 |
|ALL | FL | BOOK | 400 | 330 |
Thanks in advance,
Luis
I think you should just use grouping sets. Much simpler query and no union:
SELECT (CASE WHEN GROUPING([Vc_MONTH]) = 1 THEN 'ALL' ELSE [Vc_MONTH] END) as [Vc_MONTH],
[Vc_STATE], [Vc_PRODUCT],
SUM ([TOTAL]) as Total_Units,
SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY GROUPING SETS (([Vc_MONTH], [Vc_REGION], [Vc_PRODUCT]),
([Vc_REGION], [Vc_PRODUCT])
);
so you already have query 1:
SELECT [Vc_MONTH],
[Vc_STATE],
[Vc_PRODUCT],
SUM ([TOTAL]) as Total_Units,
SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY [Vc_MONTH],
[Vc_STATE],
[Vc_PRODUCT]
next you need to GROUP BY Month and Product correct? However, you need to specify a value in the 'Vc_STATE' column so that result sets from the two queries return the same columns.
UNION
SELECT [Vc_MONTH],
'ALL STATES',
[Vc_PRODUCT],
SUM ([TOTAL]) as Total_Units,
SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY [Vc_MONTH],
[Vc_PRODUCT]
Not a SQL Server Guru by any stretch, but I think it has a with clause:
with monthly as (
SELECT
[Vc_MONTH], [Vc_STATE], [Vc_PRODUCT],
SUM ([TOTAL]) as Total_Units,
SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY
[Vc_MONTH], [Vc_STATE], [Vc_PRODUCT]
)
select
[Vc_MONTH], [Vc_STATE], [Vc_PRODUCT],
Total_Units, Open_Units
from monthly
union all
select
'*ALL', [Vc_STATE], [Vc_PRODUCT],
sum (Total_Units), sum (Open_Units)
from monthly
group by [Vc_STATE], [Vc_PRODUCT]