Whenever we set connector to our data in Power BI, we have an option to select connection to a certain object (e.g. a table) or there is a second option. That is connection using direct query which secures live connection to data source. We will prepare simple Sales report by few clicks. (as in video). I will show you how to set a connection like that. Direct Query makes sense mainly if we connect to some database and use SQL language.

We will create a simple report using Power BI desktop. It will display sales by year through products and product categories:

- Source: SQL Server/database Temp
- Table: : udv_SalesByProducts (view created from sample database AdventureWorksDW2014)
- Report designer: Power BI desktop, load report to cloud afterwards
- Power BI Gateway: Set gateway to data source

[video width="1280" height="720" mp4="http://biportal.cz/wp-content/uploads/2017/11/Sales-report-video-tutorialmp4.mp4"][/video]

Report is loaded to power bi service. In order for Direcq query to work, it is needed to set Power BI gateway (enterprise). Therefore, we set new cluster which will lead to instance localhost and Temp database.

Příspěvek Sales Report in Power BI and Direct Query into SQL Database pochází z BI Portal

]]>Logical functions serve us to test conditions not only in Excel. They can be used in situation when we need to compare 2 or more values and find out if a certain condition is met. Often used function IF belongs to logical functions, except for this function, you will most often use functions AND and OR on which we will focus now.

Returns value TRUE or FALSE depending on the fulfillment of the conditions.

**=AND( <logical value 1>; <logical value 2>, ... )**

** Result: **We enter conditions of equality and inequality and if are the conditions inside the function fulfilled, the function returns the value TRUE.

Both functions can be combined. Let's say we need to find all records where:

- Value 1 = 1 AND
- Value 2 = 0 or value 2 = 1

In other words, Value 1 must always be 1 and value 2 can be 1 or 0.

Entry is not complicated. The formula is similar to the one from the first example. Only difference is that second argument of A function is not a simple requirement but another OR function.

Příspěvek AND, OR Logical Functions in Excel pochází z BI Portal

]]>SUBTOTAL is a little strange function. There are 11 mathematical functions hiding inside it. We can calculate sum, arithmetic mean, spread, minimum/maximum and another statistic values using SUBTOTAL function. What's more, we can limit whether we want to calculate even with hidden values which were hidden manually. All under roof of one function.

Even though this function offer a lot of possibilities, its syntax is very easy in general:

**=SUBTOTAL(<type of calculation>;<range of cells>)**

Thanks to the number which is the first argument of the function it is possible to select what calculation do we require. Following table shows overview of numeric codes and functions available.

without hidden values |
with hidden values |
function |

101 | 1 | AVERAGE |

102 | 2 | COUNT |

103 | 3 | COUNT2 |

104 | 4 | MAX |

105 | 5 | MIN |

106 | 6 | PRODUCT |

107 | 7 | STDEV |

108 | 8 | STDEVP |

109 | 9 | SUM |

110 | 10 | VAR |

111 | 11 | VARP |

Hidden values => hidden rows or columns

You will find a description for each function in help section of Excel or in our articles. So for example if we want to find out minimal value for area B8 to C22, we need to enter =SUBTOTAL(105,B8:C22). Take a look at the example.

It is possible to get many statistic values thanks to SUBTOTAL function without need for any other function. We only change number (code) in the argument. If you want to exclude hidden values from the area selection, enter 102 instead of 2 (for finding number of values) for example. Look at the table above. What is meant by hidden values are cells which are hidden by Hidden rows selection.

You may find our article on statistic functions useful if you want to refresh your basic statistic functions and how to manage them in Excel

Příspěvek Excel | Subtotal – 11 Functions in One pochází z BI Portal

]]>There is a huge number of functions for date and time in Excel. they are used to extract parts of the date or time type of cell.

You may have needed to extract some certain parts of the data of date and time data types, for example month. All functions have identical and absolutely simple syntax:

**=YEAR(<date>)**

**=MONTH(<date>)...etc**

and the syntax is unchanging for all six functions. So how does it work?

We will focus on date first of all. Let's have a date 15/8/2016 in A4 cell. If we will use formula =YEAR(A4) we get number 2016 as a result. If we type in =MONTH(A4) we get number 8 and finally, yes you guessed it right, for =DAY(A4) number 15 is the result. So each function extracts (drags out) year, month and day from the entered date. Caution, date cannot be entered as a text, as in 15th of August 2016. An error #VALUE would occur for month section.

You can read about how to get rid of error messages in article on IFERROR function usage. Date must contain all three items. Therefore for entry 12.5. the function would again return an error since this entry is not considered to be date data type.

Time functions work identically. We have time 14:52:11 entered in cell E2. You might be already guessing how will it be. For =HOUR(E2) the result will be number 14, for =MINUTE(E2) you get number 52 and finally for =SECOND(E2) you get 11 seconds, therefore number 11. What happens if we have time data 21:18 for example? No seconds included and we query using function SECOND? The result will not be an error but a number 0. Function simply assumed that if there are not any seconds entered, it is considered to be 0.

One note in the end. All functions mentioned belong to Date and time functions group. Use of these functions is simple and useful whenever we need to extract only some parts out of date and time data.

Příspěvek Excel – Function for date and time – YEAR, MONTH, DAY, HOUR, MINUTE, SECOND pochází z BI Portal

]]>It is nothing hard to create report using Power BI desktop for example from Excel and then upload it to the cloud on powerbi.com. But how to update the cloud data when the source file is located on your PC/server and "the cloud service does not see the data"? One of the ways is to update the report manually in Power BI desktop, counting with uploading it to the web again afterwards. But that is not the most handy solution. The elegant solution is an automatic update using Power BI Gateway.

Simply put, it is a safe bridge between on-premise data (data on PC or server) and Power BI service. This bridge creates a connection between your report uploaded to Power BI service and data source (e.g. some Excel). If the source Excel is not changed afterward, the changes will get mirrored into the report.

Power BI gateway can be simply installed on your server/pc. Here is the link for download.

You can select type of the gate during the installation - 2 options are available and it can be sort of confusing at the first glance

**1) On-premises data gateway** - preselected type meant for personal usage and mainly for servers. This gateway offers more options of use. Main benefit is multiple users can use it if installed on server. With according permit issued by gate admin of course.

**2) On-premises data gateway (Personal mode)** - second type is the easier personal mode. This gate is installed on PC and enables only updates of report done from account to which the gate is connected (does not allow multiple users to access the dataset). This option haves also more restrictions.

Příspěvek Power BI Gateway – Introduction Into Tool For Report Refresh/Update pochází z BI Portal

]]>It is possible to connect to many data sources using Power Query (more in this article) and SQL Server database is one of them. This article will tell you how to set connection to SQL Server table directly from Excel in few simple steps.

It is not needed to use Power Query to establish a connection with SQL Server although it is the most comfortable way. This way to import data can be found in article How to execute SQL query in Excel. But back to our topic.

We will import prepared view to SQL Server instance __localhost\MOJEINSTANCE __in database __Temp. __The view is named __udv_SalesByProducts __and it displays sales by product with time context. This view is needed to be imported into Excel together with connection to database so that we will be able to refresh the data in Excel whenever we want to.

In case you do not know where is your SQL server located or you do not hold access rights, let your IT admin help you.

**Step 1)** Select Power Query card in Excel - from database - from SQL Server database

**Step 2)** Fill in name of the server and database in which is the view located. You have an option to set SQL query to perform on the server. We will leave the field for the SQL command empty since object (view) is needed to be displayed.

**Step 3)** In the last step, all that's left to do is select object and confirm via load button. We also have an opportunity to edit data further and create steps continuity using Power query editor. These steps would perform after every update.

The table has been created on the newly created list. It contains data from SQL Server and it is possible to update it as you wish.

Příspěvek Connecting to SQL Server | Power Query pochází z BI Portal

]]>Did it ever happen to you that the Power BI update did not work? There might be several reasons for this problem and it is always user's fault. We will model a few of these situations on a timesheet. Our goal is to find out why the report refresh does not perform without an error. Problems occur mostly when working with source file types as Excel. If you are connecting on data to SQL Server database, you will probably encounter these errors less often.

It is quite common we put the report together in a hurry in power bi desktop app from Excel data for example. We then upload it to web and after a while move the source Excel somewhere else or rename it. Pbi file (power bi desktop file) obviously remembers directions to original location and will signal error during report attempt.

Original source file Projekty.xlsx will be renamed to **Projekty_.xlsx**

and an error during Refresh attempt is born: Datasource.Error: Could not find file .....

Another common reason can be a mistake in data quality in some field of data type. Data type change in 1 cell of source Excel can result in the whole report stopping to work.

I will change the date in D6 cell to nonsense value 32.10.2017 (date doesnt exists)

What will happen during the report refresh attempt? The result is error __1 of the loaded queries contained Errors __and the power bi update again does not work correctly. The report will stay in its original state.

When we click on view errors, Power query warns us about problem with upload of 1 row (the one we changed).

Another cause can be change of data type in source data, even an unwanted one. Again, small incautiousness with one cell is enough. I will change data type in cell G8 from original type number to text, as seen in the picture below.

Again, we have exactly same error

Příspěvek Power BI Refresh Does Not Work – Reason? pochází z BI Portal

]]>You have a huge table with many date entries in it. You need to find out which date is after 20th week of the year or if a certain date is Friday. MS Excel will help by three functions - WEEKNUM, ISOWEEKNUM,WEEKDAY . It is definitely more effective than searching in calendar. More so if you have a real lot of data. The premise for use of these function in Date and time group are cells containing data type Date.

Let's start with function ISOWEEKNUM. Its syntaxe is very simple:

**=ISOWEEKNUM(cell)**

and you do not need anything else. Either enter the date into quotes or link to the cell. The result of the function is the number of the week in year according to ISO 8601 standard. This means that first week of the year is the one containing at least one workday. New year must be on Thursday and 2nd of January is workday. If the New year falls on Sunday, the 1st week starts on 1st Monday.

Back to Excel. For example for formula =ISOWEEKNUM("17.6.2014") you will get 25 as result. Meaning that the entered date was in 25th week of the year. That's all. This function calculates with default Excel settings. For Czech location, it means that the week starts by Monday. But what about Anglo-Saxon lands where the week starts by Sunday? We will use function WEEKNUM.

Function WEEKNUM also returns number of the week in the year but it is possible to state which system will be used to calculate the week number. Function haves this syntax:

**=WEEKNUM(cell;type)**

and the code is determining the number of the week calculation system according to this key:

type |
Week begins with |
System |

1 či neuveden | Sunday | 1 |

2 | Monday | 1 |

11 | Monday | 1 |

12 | Tuesday | 1 |

13 | Wednesday | 1 |

14 | Thursday | 1 |

15 | Friday | 1 |

16 | Saturday | 1 |

17 | Sunday | 1 |

21 | Monday | 2 |

Lets explain this table. Type means given type in the function formula WEEKNUM. System 1 or 2 means following:

System 1 - first week marked by number 1 is the week in which the data 1st January is present

System 2 - is according to ISO 8601 norm, also called European week notation system

Parameter type is optional.

WEEKDAY function is simple. It returns serial number of day in week to entered date. Function is guided by local Excel version. Formula syntaxe is

**=WEEKDAY(cell)**

and the result is simply a serial number of the day in week. Meaning Tuesday will have 2 and Saturday 6 (according to our start of the week).

Příspěvek WEEKNUM, ISOWEEKNUM,WEEKDAY Excel – Was it on Friday After 15th Week of The Year? pochází z BI Portal

]]>FILTER functions (filtering functions) are used to get values from the table. Some functions enable additional filtering in a given table. These functions are often part of different functions as is nested parameter and enable dynamic calculations. They often serve as a value filter for function CALCULATE. Mostly it is the case of typical member of this category - FILTER function. For instance when we want to create metrics summing up values of some column but we need to limit the column based on some requirement = filter.

**Script example representatives of DAX category function FILTER **(function is named identically to the whole category). In the example of metrics is total sum of the sales > 1000

`Sales USD > 1000 =`

`SUMX (`

` FILTER (`

` Sales,`

` Sales[UnitCosts] * Sales[Quantity]`

` > 1000`

` ),`

` Sales[UnitPrice] * Sales[Quantity]`

`)`

- ADDCOLUMNS
- ADDMISSINGITEMS
- ALL
- ALLEXCEPT
- ALLNOBLANKROW
- ALLSELECTED
- CALCULATE
- CALCULATETABLE
- CALENDAR
- CALENDARAUTO
- CROSSFILTER
- CROSSJOIN
- CURRENTGROUP
- DISTINCT
- EARLIER
- EARLIEST
- EXCEPT
- FILTER
- FILTERS
- GENERATE
- GENERATEALL
- GROUPBY
- IGNORE
- INTERSECT
- ISONORAFTER
- KEEPFILTERS
- LOOKUPVALUE
- NATURALINNERJOIN
- NATURALLEFTOUTERJOIN
- RELATED
- RELATEDTABLE
- ROLLUP
- ROLLUPADDISSUBTOTAL
- ROLLUPGROUP
- ROLLUPISSUBTOTAL
- ROW
- SAMPLE
- SELECTCOLUMNS
- SUBSTITUTEWITHINDEX
- SUMMARIZE
- SUMMARIZECOLUMNS
- TOPN
- UNION
- USERELATIONSHIP
- VALUES

Příspěvek FILTER Functions for Power BI in DAX – Overview pochází z BI Portal

]]>Aggregate functions in DAX language are used to get certain __summarized or grouped data view. __Thanks to this, we can edit high detail data in a certain column into more simple and reportable form using aggregation. We can for example sum up (SUM, SUMX) some financial metrics by individual category or express maximal/minimal (MAX, MAXX, MIN, MINX) value using categories.

Functions can be divided into 2 categories. There is a difference from Excel in DAX for Power BI and PowerPivot. Some functions in DAX have an equivalent function ending in "**X**". These functions were explained in article SUM vs. SUMX.

** 1) Standard functions: **<column> is the most accepted parameter. This does not count for COUNTROWS where the parameter is <table>. These functions perform aggregation always with the whole column.

- SUM - sums up values in given column
- COUNT - count of
**numeric**values in given column - COUNTA - count of not-empty values in a column
**including text** - MAX - returns maximal value in given column
- MIN - returns minimal value in given column
- AVERAGE - average of values in given column
- AVERAGEA - works identically to function AVERAGE but it processes also non-numerical characters
- COUNTBLANK - sums up all empty values in a column
- COUNTROWS - number of rows in a table. It is mostly used in combination with function FILTER.

** 2) X Functions:** These function accept table and expression <tabulka>, <expression> as a parameter. Then iterate row by row in given table. Their meaning is same to those mentioned above.

- SUMX
- MAXX
- MINX
- COUNTX
- AVERAGEX
- COUNTX

I will use my previously created Power BI laboratory again for this demonstration. I will be working with 2 tables - Sales (FactInternetSales) and Calendar (DimDate) - as in model.

I will then create metrics using standard aggregate functions and let it display in the report.

Příspěvek Aggregation in DAX – How Aggregations Work? + Function Overview pochází z BI Portal

]]>CALCULATE is an important function for a large number of calculations and for those of you who actively do DAX formulas in Power BI or PowerPivot. Understanding of how the function works and how it is used does not look simple at first glance. And that is why this article is here.

**Syntax function: ***CALCULATE( <expression>, <filter 1>, <filter 2>… )*

__Expression:__First parameter is expression. In most of the cases it is some agreggate DAX function of type SUM, MIN, MAX, COUNTROWS or its colleague X function (e.g. SUMX).__Filter 1 - Filter n:__We then enter filter type parameter. There is plenty of filters available and it is also possible to apply some sort of AND/OR logic. Filter can be set in many ways:- As a simple condition of type Product-"auto"...
- or as a next function. Most of table function FILTER which works as a condition - it limits values

**Function example: **

Blue cars sales = CALCULATE ( SUM ( Sales[Amount] ), Product[ProductColor] = "blue" )

Caution:Expression entered into first parameter must be evaluated in a way so that the result will always be 1 value. Result of the function is not a table but a value. This is why we mostly use aggregate functions.

Lets demonstrate the function on some examples in Power BI desktop environment. I will work with four tables containing sales (FactInternetSales), Territories, Currencies (DimCurrency) and Calendar (DimDate) as seen in the model.

**Step 1) **I will start simple. Simple Power BI report with table will be prepared. New columns will be then added into it. I will display Sales by territories for now:

**Step 2) **I am interested in total sales realized in USD in the next column which will be displayed. And the last column will be a ratio between both previous columns and it will show what is the ration of sales in USD compared to all sales in %. Finally, DAX function CALCULATE comes into play:

Formula Total Sales USD:

`Total Sales USD :=`

` CALCULATE (`

` SUM ( FactInternetSales[Total Sales] ),`

` DimCurrency[CurrencyName] = "US Dollar"`

` )`

Formula % USD/Sales:

`% USD/Sales =`

`CALCULATE (`

` SUM ( FactInternetSales[Total Sales] ),`

` DimCurrency[CurrencyName] = "US Dollar"`

`)`

` / SUM ( FactInternetSales[Total Sales] )`

Result (as below) is fine and we see everything needed - total sales, sales in USD and its ratio. The result can be visualized by some graph and it is done.

Příspěvek Function DAX CALCULATE – Aggregation With a Condition in Power BI pochází z BI Portal

]]>Function REALTED is very frequent and useful in DAX language. We can use it for example when we need to search table (A) for value from other table (B). And finding the according value is the exact task of this function.

**Syntax **is very simple:

Data organization in Power BI consisting of tables connected **in data model** is the answer. And so because references exist between the tables, there is no need for the function to have multiple parameters. This is the difference compared to function VLOOKUP which needs 4 parameters.

The possibility to use the function is limited. We can use it only in case when we query into the table which returns only 1 value. In our case, we can only query from __factual table FactInternetSales. __

1) I create new column in this table and write a formula. You can see in the picture below how the function starts to __automatically offer values __from tables into which it sees.

2) I will let it add territory to every record on sales from table DimSalesTerritory using entry "RELATED(DimSalesTerritory[SalesTerritoryCountry])"

Příspěvek DAX RELATED Function for Power BI – Lookup Into Table pochází z BI Portal

]]>What about upgrading GROUP BY clause by use of useful commands? GROUP BY is used in a clause with aggregate operations. When it is used, aggregation happens throughout all columns. Using this simple "grouping" __does not enable us to do totals and subtotals. __But there is plenty of operators which can easily query for totals.

After you get to know these operators, you will be able to create SQL queries and views with total rows. That is not something anyone could do. :) All three operators work in a similar manner. So I will do ROLLUP in detail and the rest will be just fast flyby.

`SELECT [Column1], [Column2], AggregateFunction([Column3]) AS Alias`

` FROM Table`

` WHERE Condition`

` GROUP BY ROLLUP ([Column1], [Column2]) |or| CUBE ([Column1], [Column2]) |or| GROUPING SETS ([Column1], [Column2]);`

First operator is ROLLUP. It returns totals __throughout all levels__ (columns). The most effective demonstration will be on example where we will display sales in first 4 months of year 2012 and 2013 in Europe region.

`SELECT`

` [ModelRegion] AS [Region],`

` [CalendarYear] AS [Year],`

` [Month] AS [Month],`

` SUM([Amount]) AS [Amount]`

` FROM [AdventureWorksDW2014].[dbo].[vTimeSeries]`

` WHERE [CalendarYear] in (2012,2013) AND [month]<=4 AND [ModelRegion] in ('M200 Europe')`

` GROUP BY ROLLUP([ModelRegion], [CalendarYear], [Month]);`

We can see that the query returns 4 rows more and contains all subtotals and total thanks to ROLLUP.

The result can be further **upgraded by function **which is useful mainly when working with ROLLUP and similar operators. It is function GROUPING(). It accepts name of the column as parameter.

Function returns 1 or 0 depending if the given column is Total/subtotal or original base value. I will show it to you on the previous example by editing SQL query.

` SELECT`

` CASE`

` WHEN GROUPING([ModelRegion]) = 1 THEN 'Super Grand Total'`

` WHEN GROUPING([CalendarYear]) = 1 THEN (CONCAT('Total ',[ModelRegion]))`

` WHEN GROUPING([Month]) = 1 THEN CONCAT('Subtotal ',[CalendarYear])`

` ELSE 'Monthly Amount'`

` END AS [Total_Level],`

` [ModelRegion] AS [Region],`

` [CalendarYear] AS [Year],`

` [Month] AS [Month],`

` SUM([Amount]) AS [Amount]`

`FROM [AdventureWorksDW2014].[dbo].[vTimeSeries]`

`WHERE [CalendarYear] in (2012,2013) AND [month]<=4 AND [ModelRegion] in ('M200 Europe')`

`GROUP BY ROLLUP (`

` [ModelRegion]`

` ,[CalendarYear]`

` ,[Month]`

`);`

We marked each row by flag using the function depending on type of total.

Item **Super grand total** is in this case equal to total count of Europe region since we did not involve any other region. If we would have more regions, the total would be a total of the regions.

You can create your own groups through which the aggregate operations will perform. Multiple groups may be defined. I will create 2 aggregate groups on the example below

- Subtotals through region and year
- Subtotals through year and month

` SELECT`

` CASE`

` WHEN [ModelRegion] IS NULL THEN 'Group 2'`

` ELSE 'Group 1'`

` END AS [Group],`

` [ModelRegion] AS [Region],`

` [CalendarYear] AS [Year],`

` [Month] AS [Month],`

` SUM([Amount]) AS [Amount]`

`FROM [AdventureWorksDW2014].[dbo].[vTimeSeries]`

`WHERE [CalendarYear] in (2012,2013) AND [month]<=4 AND [ModelRegion] in ('M200 Europe')`

`GROUP BY GROUPING SETS (`

` ([ModelRegion],[CalendarYear]),`

` ([CalendarYear],[Month])`

`); `

This command creates totals throughout all value combinations in columns indexed as argument. To identify totals and subtotals we can use GROUPING function mentioned earlier

` SELECT`

` [ModelRegion] AS [Region],`

` [CalendarYear] AS [Year],`

` [Month] AS [Month],`

` SUM([Amount]) AS [Amount]`

`FROM [AdventureWorksDW2014].[dbo].[vTimeSeries]`

`WHERE [CalendarYear] in (2012,2013) AND [month]<=4 AND [ModelRegion] in ('M200 Europe')`

`GROUP BY CUBE (`

` [ModelRegion]`

` ,[CalendarYear]`

` ,[Month]`

`); `

Příspěvek SQL ROLLUP, CUBE, GROUPING SETS – Totals and Subtotals pochází z BI Portal

]]>You probably noticed that there are many functions in DAX that are similar to Excel ones (as in article here). Contrary to Excel functions, DAX haves on strange thing. Some aggregate functions have another __similar function ending with X __- for example SUM and SUMX or COUNT and COUNTX.

These functions often give same results but it is not easy to identify the difference at first sight. We have a little Hamlet question here.

SUM and SUMX are aggregate functions performing addition (measures). We can divide it further in DAX into 2 sexy-sounding categories - Aggregators and Iterators. Meanwhile, SUM (and her sisters COUNT etc.) belongs to category of so called aggregators, SUMX is in DAX iterator. What does that even mean?

All aggregators including SUM perform aggregation through the whole columns. If we take a look at the syntax of the function, we can see that the argument of the function is always just one parameter - <ColumnName>.

It means that it is not possible to enter any expression into the function argument. You cannot, for example, perform SUM(X*Y). It is always aggregation of some column.

SUMX is a function of multiple uses. X functions are called iterators because they do not react to the whole column as for example SUM. They __iterate by each row. __Furthermore, the function can accept expression as an argument. Let's take a look at syntax:

**SUMX([Table]; [Expression]) **

__The argument of the function is table and expression __(can be column or expression). Since the function does accept expression and iteration goes by each row of the table, the function offers 2 advantages:

- if we want to report from the table where the measures must be calculated, we do not have to create new column (calculated column). We can use SUMX directly
- Thanks to expression, new opportunities arise concerning operations of condition type (filters) etc.

The function haves 1 major disadvantage- since it is iterating,it is much more slower than its colleague SUM.It is sensible to use SUMX only in cases when it is really needed.

Let's take a look on a couple of formula examples of both functions directly in Power BI. We will work in environment with 3 tables (Sales, Territories and Calendar).

FactInternetSales contains information on new product sales, their unit price, expenses and quantity. I will create 2 new measures in table FactInternetSales and visualize the result by displaying sales through territories.

- Total Sales SUM = SUM(FactInternetSales[Sales Amount])
- Total Sales as SUMX = SUMX(FactInternetSales;FactInternetSales[UnitPrice] * FactInternetSales[Quantity])

Only difference between the functions so far is that I could perform addition in SUMX using expression FactInternetSales[UnitPrice] * FactInternetSales[Quantity]. Other than that, the result is same.

__We will add another column [Sales Year 2013]__ - and that is exactly the situation where it is needed to use SUMX function. I want to display only sales from the year 2013 and I condition aggregating by that (as in the formula in the picture). Sales not fulfilling the condition are not in the total.

Příspěvek DAX | SUM and SUMX – How to Perform Aggregation in DAX Language for Power BI pochází z BI Portal

]]>Correct table indexing in SQL Server is a base for good database performance during querying. You need to understand __how SQL Server stores data __into tables/indexes if you want to create appropriate sql indexes. It is also important to know how to approach these data correctly during querying.

__File page __is the smallest unit for reading and writing into db objects in SQL Server. Every single sql file page haves 8 KB and is related to one object (for example table or index). Individual file pages are organized into __Extents__. Each extent consists of 8 file pages.

On the highest level, data are organized into 2 file types. We will focus on the first one (MDF) in this article

**MDF files**where the data is**LDF files**of transactional log

The way of physical data storing on disk was mentioned in the introduction. Now we finally get to the logical organization. Data are stored in file pages as was already mentioned. There is plenty of them and so there must be a system by which SQL server orientates in it.

__Performance of SQL queries __depends directly on ability of SQL engine to give out individual file pages to some table which we are trying to query. This performance depends on how are the tables logically organized.

We use special system objects called Index allocation map (IAM) for these purposes. Each table haves assigned at least one such object. These objects work on linking principle - it links individual file pages with tables.

According to number of IAM in a table, we distinguish 2 organization methods - Heap and balanced tree

Heap means tables which __are not organized in any way __:) and have only 1 IAM (so called first IAM). It is like going for a book to the library which is completely unorganized. You would have to go through all the books to find the one you are looking for.

You will get a table of this type whenever you create it without the primary key or __without indexes__. It is simply a heap of unorganized file pages. SQL server must scan whole heap if we query such table with a condition or try JOIN with another table. This means every page file is scanned separately - and this takes a long while.

[caption id="attachment_2977" align="alignnone" width="556"] Source: Itzik Ben-Gan, Dejan Sarka, Ron Talmage. Querying Microsoft SQL Server 2012. Microsoft press, Vydání 2012. 752 stran. ISBN 0735666059[/caption]

On the other hand, data organization such as balanced tree is something completely different. Table gets organized as a balanced tree anytime you create clustered index (i.e. primary key) above the table.

This __architecture creates clusters __and therefore is the scan for the records much faster. SQL Server does not have to scan whole table as in heap case. It searches individual clusters. Sql indexes therefore work like if you search library for books by genre and author.

[caption id="attachment_2988" align="alignnone" width="822"] Source: Itzik Ben-Gan, Dejan Sarka, Ron Talmage. Querying Microsoft SQL Server 2012. Microsoft press, Vydání 2012. 752 stran. ISBN 0735666059[/caption]

Next articles will have me look at index fragmentation and I will also demonstrate, how to repair indexes automatically using SQL script

Příspěvek SQL Indexes – Indexing Theory Simply, Balanced Tree, Heaps pochází z BI Portal

]]>We can create two types of new values using DAX in Power BI or PowerPivot. They are calculated measures and columns. Difference between measure and column and its understanding is another major step in order to understand DAX language for Power BI.

New measure or column in Power BI is added by clicking right mouse button above any column. Then select New measure or New column (as seen in the picture).

__Column - __adding new column into which is later inserted the DAX formula results in the formula being processed on the table level row by row. All column values get summed up on the report level. Created column can then be seen directly in the table.

__Measure - __ The effect is different with measures. We do not see it by the table as a new column but as a field in the selection below the table. In report, the measure is calculated on the report level in the context in which we display it.

This table should help to understand the difference. We will work with these tables during the demonstration.

__Sales (FactInternetSales)____Teritories (DimSalesTerritory)__, in which the sales occur - geographical information on sales. Meaning where the sales took place.__Calendar (DimDate)__, contains dates and information such as month, year etc.

**Step 1) **Add new column with total sales into the table with sales (price * amount). It already contains individual item prices, production costs and sold amounts.

**Step 2) **We will create simple power bi visualization where I will display running result. Sales will be displayed by territory.

**Step 3) **We will now illustrate the difference between measures and columns. First, add a new column via DAX formula (as in picture below). It will show ratio of unit costs and unit price = UnitCosts/UnitPrice . Then display the result in report.

The result is nonsense(as in the picture) - what happened is that percentage shares got counted up throughout all rows

**Step 4) **We need to create not a new column but a DAX measure in order to get the report working correctly. Measure is calculated all the way at the report level. Formula for the measure will be similar only with the exception of aggregate function usage. Costs and price ratio - SUM(UnitCosts)/SUM(UnitPrice). The result is exactly as we need it. ** **

I hope that the difference between measure and column in creating new values was explained well enough.

Příspěvek DAX Difference Between Measure and Column + Example on Report pochází z BI Portal

]]>DAX language (**D**ata **A**nalysis E**x**pressions) servers for data management and editing prior reporting in Power BI or Powerpivot, similarly to __function set __in Excel. This language is able to return values based on use of functions. We can either **enrich, add or analyze **data prior reporting in Excel, and DAX enables us to do the same.

DAX language have an advantage for beginners since the visual similarity to Excel is significant - plenty of functions are named identically and do the same thing. But is I mentioned, there are also big differences between DAX and Excel. Let's take a look at them.

**Purpose -**both tools help to work with data and add or analyze them based on formulas**Similar functions -**many functions have identical names as in Excel (obviously in English) and functions can therefore be learned quicker. For example YEAR, WEEKDAY, AND, IF etc.**Function categories -**functions are, as in Excel, divided into categories which are pretty similar to each other. Below are listed categories and examples of the functions. You will see identical/similar ones if you know Excel functions by heart.__Date and time__- DATE, EOMONTH, TODAY()__Information__- ISERROR, ISNUMBER, ISTEXT__Logical__- IF, IFERROR, AND__Math__- ABS, POWER, ROUND__Statistical__- AVERAGE, MAX, COUNTBLANK__Text__- CONCACENATE, LEFT, SEARCH__(!) Time intelligence__- ENDOFMONTH, NEXTQUARTER, DATESBETWEEN__(!) Filter__- DISTINCT, FILTER, RELATEDTABLE

- DAX functions work as in a database apart from the Excel - we are used to percept excel cell or area as a calculation unit or a reference for a formula in Excel or other table processor. For example, we can have different formulas in one column. It is not possible like that in DAX.
__The formula always influences the whole column__and each row of the table haves the same formula. - DAX can return table as a result of the function
- Primary determination - DAX is used when working with table and columns whereas Excel works with values
- Data model - We need to set table references (column connections) if we work with multiple tables in powerpivot. This connection must be set up if we use DAX function which aggregates or searches for value in a different table.

First import some data (i.e. from excel) to Power BI desktop. We will get to the command line for formulas by opening imported table and creating new column. Example to demonstrate this is shown below. We work with the calendar table and type in a simple function which calculates Year from "Date" field. You can learn how to work with formula parameters in article Parameters - Introduction into DAX expressions.

Now you only need to widen your knowledge of functions and train DAX language for Power BI.

Příspěvek DAX language – Introduction into DAX for Power BI and Powerpivot pochází z BI Portal

]]>We newly added section Business intelligence E-books. You can find freely available education materials here. The section is accessible via menu or by link https://biportal.cz/en/e-books/

Příspěvek New E-books Section for Download pochází z BI Portal

]]>DAX language haves its standards as does any other language. This article sums up and describes how look the parameters of formulas. In other words, this article is about function arguments overview. It is good to know parameter categories appearing in this language to effectively use parameters in DAX.

__Demonstration__ - in the screenshot is function LOOKUPVALUE. It is equivalent of Excel function VLOOKUP. The function works similarly. It returns column value in table A based on match of other column in table A with value in table B.

__Parameters __in this case are:

__Result_ColumnName__- name of the column containing the value wanted to be returned using function__Search_ColumnName__- name of the column in which the value is located and based on which we search for match in column Result_ColumnName__Search_Value__- value based on which we search for match

**1) Expression - **call sign for an expression returning scalar value. Each expression is evaluated for each row of the table. Lower is the example of sum function. It calculates total sum of all sales amounts in a table.

`[Total Sales] =`

`SUM (`

**[Sales_Amount]**)

But the expression can be only:

`[Total Sales] =`

**[Sales_Amount]*2**

**2) Table - **expression returning other table as a result. In the example, it can be seen how to generate list of dates between 2 two bordering dates. All done using function CALENDAR.

`[Calendar] := `

**CALENDAR(DateStart; DateEnd)**

**3) TableName - **name of an existing table. In this case is the name of the table argument of function SUMX. We perform sum in table "Table_With_Sales" of expression Amount after discount.

`[Amount after discount] :=`

`SUMX (`

**[Table_With_Sales]**;`[Sales_Amount]* ( 1 - [Discount %] ))`

**4) Column name - **similar to 3) TableName - it is a name of column in existing table. It is being used as function argument - for example already mentioned LOOKUP

**5) Name - **Text constant expressing name of new object (column in a table)

**6) Order - **In functions, it is possible to use parameter which for any reason organizes our data in descending (DESC) or ascending (ASC) order. Example of this can be function TOPN. It returns TOP X values from any table organized by some value. We need to tell it by what it should guide itself (ASC/DESC) so it will be possible for it to return result of TOP X values.

`= TOPN (15,Employees,Employee[LastName],`

**ASC)**

**7) Ties - **Parameters characteristic for many advanced functions. According Excel equivalent might be last logical function parameter VLOOKUP (approximate/exact value)

Příspěvek Parameters in DAX – Introduction into DAX Expressions pochází z BI Portal

]]>What I have here today is a article for beginners. It will be full of sql query examples. We will start from the most basic ones and continue up. SQL queries will be organized chronologically according to their difficulty. I will add more later on.

SQL knowledge is a must for any IT department nowadays. But these days __not only IT guys__ but also analytics and people working with reports __use SQL querying __since it is making their work much more effective.

__Examples of how can SQL query help you with your work: __

- You can load data from a database to Excel and analyze it in contingent table - more in article How to launch SQL query in Excel
- Connect to database in Power BI and create some cool report
- Connect to Excel database via Power Query - Connecting to SQL server in Power Query
- Many other uses

Query with all columns selection using * without limiting conditions (tutorial on select clause here):

`SELECT `

`*`

` FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts];`

Query with limiting condition on year 2010 in where (tutorial on where clause here):

`SELECT *`

` FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]`

` WHERE [Year] = 2010;`

SQL query in which we used many conditions and some basic operators IN, BETWEEN, LIKE and >

`SELECT *`

` FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]`

` WHERE [Product Subcategory] LIKE ('%bike%')`

` AND [Year] IN (2013,2014)`

` AND [Month] BETWEEN 1 AND 6`

` AND [AverageAmount] > 0;`

We select 10 (TOP) highest sales in the year 2013 organized in descending order (ORDER BY <Column> DESC)

`SELECT TOP 10 *`

` FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]`

` WHERE [Year] = 2013`

` ORDER BY [Amount] DESC;`

We apply aggregation functions SUM, COUNT, AVG, MAX, MIN on sales through calendar year. Similar sql queries with use of at least 1 aggregation function are usual:

`SELECT`

` [Year],`

` SUM([Amount]) AS [Amount],`

` COUNT(*) AS [Sales Count],`

` AVG([Amount]) AS [Average Amount],`

` MAX([Amount]) AS [Max Amount]`

`,`

` MIN([Amount]) AS [Min_Amount]`

` FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]`

` GROUP BY [Year]`

` ORDER BY [Year] ASC;`

SQL query on how limit result on records fulfilling the condition after aggregation by using having (we will use base of query as in example 5)

`SELECT`

` [Year],`

` AVG([Amount]) AS [Average]`

` FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]`

` GROUP BY [Year]`

`HAVING AVG([Amount]) < 5000`

` ORDER BY [Year] ASC;`

Příspěvek Basic SQL Queries – Overview For Beginners With Examples pochází z BI Portal

]]>