Create a column of sum of values after grouping two columns in Power BI - sum

I am trying to sum values of third column based on first two columns and enter in new column.
Day Product type price total
1/1/2019 A1 T1 3 8
1/1/2019 A1 T2 5 8
1/2/2019 A2 T1 2 3
1/2/2019 A2 T2 1 3
1/1/2019 B1 T1 4 12
1/1/2019 B1 T2 7 12
1/2/2019 B2 T1 3 5
1/2/2019 B2 T2 2 5
1/3/2019 A1 T2 2 8
1/4/2019 A2 T1 9 11
1/3/2019 B1 T1 6 11
1/3/2019 B1 T2 5 11
1/4/2019 B2 T1 4 4
Total is sum of price regardless of type and unique as combination of date Product. check these excel columns

It is normally not recommended to add a column for summarized values. Summarization is supposed to be done with measures.
It is very easy to get the Total for each Day and Product. First you will define a measure. In the Modeling tab, click New Measure and type Total = SUM(Sales[Price]). I'm assuming the name of your table to be "Sales", so you need to replace it with your own table name.
Then in the report, choose an appropriate visualization and drag and drop Day, Product, and Total. The measure Total calculates the sum of Price for each Day and Product on the fly.
It is also possible to keep the Total of Day and Product in a column inside the model. However, this is not a best practice. Before doing this, try to find a way with measures, and only do this if you are an experienced user and you know there is some good reason to do this.
In this case, in the Modeling tab, click New Column and input this formula.
Total of Day and Product = CALCULATE(
SUM(Sales[Price]),
ALLEXCEPT(Sales, Sales[Day], Sales[Product])
)

Go to the Edit Queries > Add Column > Custom Column and use something like this:
= if [Product] = "A1" and [type] = "T1" then [price] * [total] else [price] * [total] * 2
This calcualation is just an example how its done because you didnt provide any information what your criterias are to sum the values in the third column. But with this example you should be able to create your new column by yourself.

Related

Rolling sum for last 3 hour records of just one column in SAS

Everyone,
What I need is to calculate for every record (every row) for the last 3 hour sum of usage (Usage is one of the columns in dataset) grouped by User and ID_option.
Every line(row) represent one record (one hour have about million records). For example I made a table with just a few records (including desired column sum_usage_3 hour):
User ID_option time usage sum_usage_3hr
1 a1 12OCT2017:11:20:32 3 10
1 a1 12OCT2017:10:23:24 7 14
1 b1 12OCT2017:09:34:55 12 12
2 b1 12OCT2017:08:55:06 4 6
1 a1 12OCT2017:07:59:53 7 7
2 b1 12OCT2017:06:59:12 2 2
I have tried with something like this code below and it returns me a sum of all time, not just the last 3 hour. I'm not surprised, but I have not so much idea how I'm going to do this in SAS.
proc sql:
CREATE table my_table
SELECT *, SUM(usage) AS sum_usage_3hr
FROM prev_table WHERE time BETWEEN TIME and intnx('second', time, -3*3600)
GROUP BY User, ID_option;
RUN;
Any help is welcomed, thanks. It's not necessary to do this in proc sql, data step is also acceptable if it's possible. I just assume that I need some kind of partition by.
Thanks in advance.
Why not just use a correlated sub-query to get the sum?
data have ;
input user id_option $ datetime :datetime. usage expected ;
format datetime datetime20.;
cards;
1 a1 12OCT2017:11:20:32 3 10
1 a1 12OCT2017:10:23:24 7 14
1 b1 12OCT2017:09:34:55 12 12
2 b1 12OCT2017:08:55:06 4 6
1 a1 12OCT2017:07:59:53 7 7
2 b1 12OCT2017:06:59:12 2 2
;
proc print; run;
proc sql ;
create table want as
select a.*
, (select sum(b.usage)
from have b
where a.user=b.user and a.id_option=b.id_option
and b.datetime between intnx('hour',a.datetime,-3,'s') and a.datetime
) as usage_3hr
from have a
;
quit;
Results
usage_
Obs user id_option datetime usage expected 3hr
1 1 a1 12OCT2017:11:20:32 3 10 10
2 1 a1 12OCT2017:10:23:24 7 14 14
3 1 b1 12OCT2017:09:34:55 12 12 12
4 2 b1 12OCT2017:08:55:06 4 6 6
5 1 a1 12OCT2017:07:59:53 7 7 7
6 2 b1 12OCT2017:06:59:12 2 2 2
The result is not surprising, as the condition for the WHERE clause is always true (time is necessarily greater or equal (or lesser or equal) to time).
I believe the simplest way would be to join the table on itself, and select the relevant rows this way:
proc sql;
create table want as
select distinct a.*
,sum(b.USAGE) as sum_usage_3hr
from have as a
left join have as b
on a.USER = b.USER
and a.ID_OPTION = b.ID_OPTION
and b.TIME between intnx('hour', a.TIME, -3) and a.TIME
group by a.USER, a.ID_OPTION, a.TIME;
quit;

Need help on writing sql select statement

I have a question about selecting rows from a table.
For example I have a table which includes information about stock movements. I wish to select the product code, latest date, latest price of each product in the table.
Stock movements table
Stock Movement Code Product code Date Price
1 1 15.06.2015 9$
2 2 17.06.2015 10$
3 2 18.06.2015 8$
4 1 19.06.2015 7$
5 3 20.06.2015 11$
6 2 21.06.2015 12$
7 3 22.06.2015 13$
I wish to select the latest date,latest price of each product from above table.
For this example I am waiting a result like below.
Stock Movement Code Product code Date Price
4 1 19.06.2015 7$
6 2 21.06.2015 12$
7 3 22.06.2015 13$
Could you please help me to write the SQL of this select statement?
Try this:
SELECT sm.*
FROM stock_movement sm
WHERE NOT EXISTS(
SELECT 'NEXT'
FROM stock_movement sm2
WHERE sm2.[Product code] = sm.[Product code]
AND sm2.date > sm.date
)

Create duplicate records in a query for MS Access

I have the following table in a Microsoft Access Database:
TableName: Cabinets
RoomID - Number
Wall ID - Number
Cabinet ID - Number
Width - Number (double)
Height - Number (double)
Depth - Number (double)
Quantity - Number
What I need to do is create a query that will duplicate each row for a number of times specified in the Quantity field. As an example, let's say that I have the following data:
Room ID Wall ID Cabinet ID Width Height Depth Quantity
1 1 1 30 34.5 24 1
1 1 2 42 34.5 24 1
1 1 3 18 34.5 24 2
I need to have a query that would create the following:
Room ID Wall ID Cabinet ID Width Height Depth
1 1 1 30 34.5 24
1 1 2 42 34.5 24
1 1 3 18 34.5 24
1 1 3 18 34.5 24
Now, I have seen, in other questions, that I can create a 'numbers' table to accomplish this, unfortunately, I can't change the table at all. In fact, I am very limited to what I can actually do with this database.
Here is what I can do:
Create a Query that will pull the data
Create a Query that will add a 'view' to the database at runtime (before the query to pull the data is run)
Any help that can be given would be greatly appreciated. Thank you very much in advanced.
Well, this is incredibly painful in Access, but you can create a numbers table on the fly. Let's assume that cabinet_id is really a unique id in the cabinets table.
select c.*
from cabinets c left join
(select (select count(*) from cabinets c2 where c2.cabinet_id <= c.cabinet_id) as n
from cabinets c
) n
on n.n <= c.quantity;
This uses the cabinets table to generate a list of numbers, using a correlated subquery to get the numbers. Note that this assumes that quantity is always less than the number of rows in this table.
If you know the ids have no gaps and start at 1, you can simplify this to:
select c.*
from cabinets c left join
(select cabinet_id) as n
from cabinets c
) n
on n.n <= c.quantity;

Efficient ways to count the number of times two items are ordered together

I am currently stuck on a problem where I have to write a SQL query to count the number of times a pair of items is ordered together.
The table that I have at my disposal is something like:
ORDER_ID | PRODUCT_ID | QUANTITY
1 1 10
1 2 20
1 3 10
2 1 10
2 2 20
3 3 50
4 2 10
I am looking to write a SQL query that can, for every unique pair of items, count the number of times they were ordered together and tell me the quantities when they were in the same order.
The resulting table should look like:
PRODUCT_ID_1 | PRODUCT_ID_2 | NUM_JOINT_ORDERS | SUM_QUANTITY_1 | SUM_QUANTITY__2
1 2 2 20 40
1 3 1 10 10
2 3 1 20 10
Some things to exploit are that:
Some orders only contain 1 item and so are not relevant in counting the pairwise relationship (not sure how to exclude these but maybe it makes sense to filter them first)
We only need to list the pairwise relationship once in the final table (so maybe a WHERE PRODUCT_ID_1 < PRODUCT_ID_2)
There is a similar post here, though I have reposted the question because
I really want to know the fastest way to do this since my original table is huge and my computational resources are limited, and
in this case I only have a single table and no table that lists the number.
You may use the following approach, which gives you the result shown above.
select
PRODUCT1, PRODUCT2, count(*), sum(QUANTITY1), sum(QUANTITY2)
from (
select
T1.PRODUCT_ID AS PRODUCT1,
T2.PRODUCT_ID AS PRODUCT2,
T1.QUANTITY AS QUANTITY1,
T2.QUANTITY AS QUANTITY2
from TABLE as T1, TABLE as T2
where T1.ORDER_ID=T2.ORDER_ID
and T1.PRODUCT_ID<T2.PRODUCT_ID
)
group by PRODUCT1, PRODUCT2

VBA - SQL update rows matching with columns

I'm using MS Access and currently am using VBA and have 2 tables right now. I am trying to insert/update rows in one table from the other. The difference is one table has the data in row direction and the other in column direction, here's the first table (Table A) with first row as headers:
Item_Num Cost YearMonth
1 2 201101
2 5 201102
And here's how I would like this table updated (Table B) with first row as headers:
Item_Num 201101 201102 201103 201104 ...
1 2 2 2 2
2 1 5 5 5
In Table A, the YearMonth of 201101 is Cost of 2 for Item_Num 1. So Table B would be updated for 201101 and on (to the last YearMonth column) for Item_Num 1 with a Cost of 2.
I've been able to update the first YearMonth it appears (201101 in the previous example), but can't seem to get the remaining columns.
Well I seem to have gotten it. I decided to create a temporary table that stored that values I wanted to update for Table B, it's a bit messy but it works.