Create DAX measure to get max sales from 2 specific years - powerpivot

I want to create a calculated measure that shows Max(Sales Year 2012, Sales Year 2013) for particular set of product. can we do this using the calculate function?
Prod 2010 2011 2012 2013
A 280 152 251 387
B 115 381 207 304
C 265 138 394 210
D 483 135 145 464
E 304 134 193 342

Sure, that's one of the benefits of using CALCULATE!
Here is the formula for MAX sales in 2013:
CALCULATE([SalesSum],Sales_New[Year] = 2013)
[SalesSum] represent just a sum of all sales and to make things a bit more organized, it's formula is very simple: SUM(Sales_New[Sales]).
Sales_New[Year] is the name of the column that includes years (see below for more details).
For 2012, simply change the filter in the second argument:
CALCULATE([SalesSum],Sales_New[Year] = 2012)
One important thing though -- I needed to transform your source data so that I can do all calculation need. I named the table Sales_new. Attached screenshot should explain everything.

Related

Dynamically Calculate difference columns based off slicer- POWERBI

I have a table with quarterly volume data, and a slicer that allows you to choose what quarter/year you want to see volume per code for. The slicer has 2019Q1 through 2021Q4 selections. I need to create dynamic difference column that will adjust depending on what quarter/year is selected in the slicer. I know I need to create a new measure using Calculate/filter but am a beginner in PowerBI and am unsure how to write that formula.
Example of raw table data:
Code
2019Q1
2019Q2
2019Q3
2019Q
2020Q1
2020Q2
2020Q3
2020Q4
11111
232
283
289
19
222
283
289
19
22222
117
481
231
31
232
286
2
19
11111
232
397
94
444
232
553
0
188
22222
117
411
15
14
232
283
25
189
Example if 2019Q1 and 2020Q1 are selected:
Code
2019Q1
2020Q1
Difference
11111
232
222
10
22222
117
481
-364
11111
232
397
-165
22222
117
411
-294
Power BI doesn't work that way. This is an Excel pivot table setup. You don't have any parameter to distinguish first and third or second and fourth row. They have the same code, so Power BI will aggregate their volumes. You could introduce a hidden index column but then why don't you simply stick to Excel? The Power BI approch to the problem would be to unpivot (stack) your table to a Code, Quarter and a Volume column, create 2 independent slicer tables for Minuend and Subtrahend and then CALCULATE your aggregated differences based on the SELECTEDVALUE of the 2 slicers.

SQL: How to remove & update records in a table, sync'ing with a (Q)List data in order to decrease the burden on database/table?

I am using Qt and MS-Sql Server on Windows7 OS.
What I have is an MS-SQL database that I use to store data/info coming from equipment that is mounted in some vehicles.
There is a table in the database named TransactionFilesInfo - a table used to store information about transaction files from the equipment, when they connect to the TCP-server.
We are using this table as we are requested to avoid duplicate files. It happens (sometimes) when the remote equipment does NOT delete the transaction files after they are sent to the server. Hence, I use the info from the table to check [size and CRC] to avoid downloading duplicates.
Some sample data for TransactionFilesInfo table looks like this:
[TransactionFilesInfo]:
DeviceID FileNo FileSequence FileSize FileCRC RecordTimeStamp
10203 2 33 230 55384 2015-11-26 14:54:15
10203 7 33 624 55391 2015-11-26 14:54:15
10203 2 34 146 21505 2015-11-26 14:54:16
10203 7 34 312 35269 2015-11-26 14:54:16
10203 2 35 206 23022 2015-11-26 15:33:22
10203 7 35 208 11091 2015-11-26 15:33:22
10203 2 36 134 34918 2015-11-26 15:55:44
10203 7 36 104 63865 2015-11-26 15:55:44
10203 2 37 140 35466 2015-11-26 16:20:38
10203 7 37 208 62907 2015-11-26 16:20:38
10203 2 38 134 17706 2015-11-26 16:38:33
10203 7 38 104 42358 2015-11-26 16:38:33
11511 2 21 194 29913 2015-12-02 16:22:59
11511 7 21 114 30038 2015-12-02 16:22:59
On the other hand, every time a device connects to the server, it first sends a list of file information. The Qt application takes care of that.
The list contains elements like this:
struct FileInfo
{
unsigned short FileNumber;
unsigned short FileSequence;
unsigned short FileCRC;
unsigned long FileSize;
};
So, as an example (inspired by the table above) the connected device (DeviceID=10203) may say that it has the following files:
QList<FileInfo> filesList;
// here is the log4qt output...
filesList[0] --> FileNo=2 FileSeq=33 FileSize=230 and FileCRC=55384
filesList[1] --> FileNo=2 FileSeq=34 FileSize=146 and FileCRC=21505
filesList[2] --> FileNo=7 FileSeq=33 FileSize=624 and FileCRC=55391
filesList[3] --> FileNo=7 FileSeq=34 FileSize=312 and FileCRC=35269 ...
Well, what I need is a method to remove/delete, for a given DeviceID, all the records in the TransactionFilesInfo table, records that are NOT in the list sent by the remote device. Hence, I will be able to decrease the burden (size) on the database table.
Remark: For the moment I just delete (#midnight) all the records that are older than let's say 10 days, based on RecordTimeStamp field. So, the size of the table doesn't increase over an alarming level :)
Finally, to clarify it a little bit: I would mainly need help with SQL. Yet, I would not refuse any idea on how to do some related things/tricks on the Qt side ;)
The SQL to delete those records might look something like this:
DELETE FROM [SAMPLE DATA]
WHERE DeviceID = 10203
and 'File' + CONVERT(varchar(11),FileNo) + '_' +
RIGHT('000' + CONVERT(varchar(11),FileSequence),3)
NOT IN ('File2_033','File2_034','File7_033','File7_034',...)
If you wanted to delete all of them for a device, you could drop the code that looks at the FileNo and FileSequence so it is simply:
DELETE FROM [SAMPLE DATA]
WHERE DeviceID = 10203

slowly changing dimension in MDX/OLAP

I have got an OLAP cube that has purchased per date, per vendor and some other dimensions.
Below a sample of the data. The vendor is identified by the unique id VendorID:
Date CCID GLID CatID VendorID Amount
31-3-2012 659 55 25 807 124.5
14-5-2012 425 74 1 1452 371.53
1-4-2012 353 55 106 1648 26.79
2-7-2012 339 78 25 1275 1208
8-7-2012 460 66 41 4311 763.25
The vendor itself has a score with values 1-good, 2-average, 3-poor, 4-unattended. These scores vary over time.
Example for vendor 807:
VendorID VendorIDDate Score
807 1-1-2012 4-unattended
807 27-2-2013 2-average
807 1-4-2014 3-poor
807 31-12-2014 1-good
Now when I start a query I would like to count the number of vendors per Score for a specific slicer on GLID, CCID and CatID on a certain date.
What is the best way to model this?
I know I can add the score to the basic fact table using a look-up for each date, but I assume there is a much better way.

How to calculate sum of the years into a new column for Current to previouse years

I am using SQL Server 2012,
How can I achieve following(Table 2) output from table 1 with sql script/tsql?
Table 1 :Current scenario
--------------------------------------------------
Year ReferredEachYear ActiveEachYear
--------------------------------------------------
2014 297 179
2013 321 144
2012 354 123
2011 317 90
2010 292 72
--------------------------------------------------
Table 2 : Expected OutPut
-------------------------------------------------------------------------
Year ReferredEachYear ActiveEachYear TotalActiveInSystem
-------------------------------------------------------------------------
2014 297 179 608
2013 321 144 429
2012 354 123 285
2011 317 90 162
2010 292 72 72
--------------------------------------------------------------------------
If you are using SQL Server 2012, then you can use the cumulative sum function:
select cs.*,
sum(cs.ActiveEachYear) over (order by cs.[year]) as TotalActiveInSystem
from CurrentScenario cs
order by cs.[year] desc;
This function is not available in earlier versions of SQL Server. You would have to use some other method in those versions (I would use a correlated subquery).
SELECT T.Year,
T.ReferredEachYear
T.ActiveEachYear
(SELECT SUM(ActiveEachYear) FROM YourTable WHERE YourTable.Year <= t.Year) AS TotalActiveinSystem
FROM yourTable T
Just as Gordon already specified, in SQL Server 2012 you can use cumulative sum function. To make it easier to understand the query can be rewritten as follows.
SELECT cs.*
,SUM(cs.ActiveEachYear) OVER
( ORDER BY cs.[year]
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS TotalActiveInSystem
FROM CurrentScenario cs
ORDER BY cs.[year] DESC;
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW this statement is referred to as window framing. This query and one in #Gordon's answer will produce same results. When you only specify ORDER BY you are implicitly defining the window frame for the calculation.
By using framing definition you can limit rows that are affected by operation.
Entire documentation on OVER() clause http://technet.microsoft.com/en-us/library/ms189461.aspx

Selecting data for a single month in ssrs pie chart

I have the following dataset for my ssrs report :
Year Month Sales1 Sales2
2013 3 29 299
2013 4 34 343
2013 5 43 322
2013 6 34 343
I need to create a pie chart with only one month of data and the month is selected using a parameter.
Example : If I select month 6 from the parameter .
I would like to show 34 and 343 within the pie chart.
I have looked at numerous tutorials and have been trying to do it since the past one hour but couldnt make
any progress.Any help is appreciated .
Filter it inside dataset properties. Here is a link
http://technet.microsoft.com/en-us/library/ms156270(v=sql.100).aspx
also here
http://technet.microsoft.com/en-us/library/aa337401(v=sql.105).aspx