Docsity

Computational Tools <3, Appunti di Elementi di Informatica

Toggle sidebar
bg1
Formulas
ALL( [<table> | <column>[, <column>[, <column>[,…]]]] )
Returns all the rows in a table, or all the values in a column, ignoring any filters that might have
been applied. This function is useful for clearing filters and creating calculations on all the rows in
a table.
DATE(<year>, <month>, <day>)
Returns the specified date in datetime format.
DISTINCTCOUNT(<column>)
Counts the number of distinct values in a column.
DIVIDE(<numerator>, <denominator> [,<alternateresult>])
Performs division and returns alternate result or BLANK() on division by 0.
• alternateresult is the value returned when division by zero results in an error. When not
provided, the default value is BLANK(). (optional)
FILTER(<table>,<filter>)
Returns a table that containing only the filtered rows.
LEFT(<text>, <num_chars>)
Returns the specified number of characters from the start of a text string.
• The text string from which you want to extract the characters, or a column containing text.
• num_chars is the number of characters you want LEFT to extract; if omitted, 1.
MID(<text>, <start_num>, <num_chars>)
Returns a string of characters from the middle of a text string, given a starting position and length.
• The text string from which you want to extract the characters, or a column containing text.
• start_num is the position of the first character you want to extract. Positions start at 1.
• num_chars is the number of characters to return.
RIGHT(<text>, <num_chars>)
RIGHT returns the last character or characters in a text string, based on the number of characters
you specify.
• The text string from which you want to extract the characters, or a column containing text.
• num_chars is the number of characters you want RIGHT to extract; if omitted, 1.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Anteprima parziale del testo

Scarica Computational Tools <3 e più Appunti in PDF di Elementi di Informatica solo su Docsity!

Formulas

ALL( [ | [, [, [,…]]]] ) Returns all the rows in a table, or all the values in a column, ignoring any filters that might have been applied. This function is useful for clearing filters and creating calculations on all the rows in a table. DATE(, , ) Returns the specified date in datetime format. DISTINCTCOUNT() Counts the number of distinct values in a column. DIVIDE(, [,]) Performs division and returns alternate result or BLANK() on division by 0.

  • alternateresult is the value returned when division by zero results in an error. When not provided, the default value is BLANK(). (optional) FILTER(,) Returns a table that containing only the filtered rows. LEFT(, ) Returns the specified number of characters from the start of a text string.
  • The text string from which you want to extract the characters, or a column containing text.
  • num_chars is the number of characters you want LEFT to extract; if omitted, 1. MID(, , ) Returns a string of characters from the middle of a text string, given a starting position and length.
  • The text string from which you want to extract the characters, or a column containing text.
  • start_num is the position of the first character you want to extract. Positions start at 1.
  • num_chars is the number of characters to return. RIGHT(, ) RIGHT returns the last character or characters in a text string, based on the number of characters you specify.
  • The text string from which you want to extract the characters, or a column containing text.
  • num_chars is the number of characters you want RIGHT to extract; if omitted, 1.

RELATED() Returns a related value from another table. SUM() Adds all the numbers in a column. SUMX(, ) Returns the sum of an expression evaluated for each row in a table. USERELATIONSHIP(,) Specifies the relationship to be used in a specific calculation as the one that exists between columnName1 and columnName2.

Selling Days How to find the number of selling days : ✅CalendarYear ✅Revenues Selling Days= DISTINCTCOUNT (Sales[OrderDate])

Formula explained

We don’t have a clear indication of when we were open, so we do a proxy. So, let’s assume that if we were open, at least we received an order.

  • The formula starts with the DISTINCTCOUNT function, which counts the number of distinct or unique values in a column.
  • The argument of the DISTINCTCOUNT function is Sales[OrderDate] , which refers to the OrderDate column in the Sales table. For example, if the "Sales" table has orders on 10 different dates, the formula will return the value 10 for "Selling Days".

AND – OR AND (&&) and OR (||) are two logical operators that allow us to verify the simultaneous holding of multiple things. Compute the revenues with 3 conditions that must be true at the same time :

  • The customer is female
  • And the revenues of 2001
  • And the customer has a bachelor’s degree ✅Color ✅Revenues Revenues to F Customers in 2001 Bachelors= SUMX ( FILTER( Sales, RELATED(Customer[Gender]=”F” && RELATED(,Calendar’[CalendarYear])=2001 && RELATED(Customer[EnglishEducation])=”Bachelors” ), Sales[UnitPrice]*Sales[OrderQuantity] )

The formula explained

The formula calculates the total sales amount for all orders made by female customers with a bachelor's degree in the year 2001:

  1. The FILTER function looks at the Sales table and return a tiltered table that only includes the 3 specified conditions. For example: RELATED(Customer[Gender]=”F” filters the Sales table to include only rows where the related Customer table has a value of "F" in the Gender column.

Revenues to F Customers OR (in 2001 AND to Bachelors)= SUMX ( FILTER( Sales, RELATED(Customer[Gender]=”F” || (RELATED(,Calendar’[CalendarYear])=2001 && RELATED(Customer[EnglishEducation])=”Bachelors”) ), Sales[UnitPrice]*Sales[OrderQuantity] )

The formula explained

The code is the same as before, the only thing that changes is that we substituted the second || with &&. Moreover, we put the 6th^ and the 7th^ line in brackets ().

Intro to CALCULATE CALCULATE is useful to simplify our calculations. It allows us to make a calculation modifying a basic measure. Let’s check the basic measure “Revenues” : ✅Color ✅Revenues Revenues= --SUM(Sales[SalesAmount]) SUMX( Sales, Sales[UnitPrice]*Sales[OrderQuantity] )

The formula explained

  • The first part of the formula, --SUM(Sales[SalesAmount]) , calculates the total sales revenue based on the SalesAmount column in the Sales table. The double negative ( -- ) is used to convert any non-numeric values in the column to 0 before summing.
  • The second part of the formula, SUMX(Sales, Sales[UnitPrice]Sales[OrderQuantity])* , uses the SUMX function to iterate through each row in the Sales table, multiplying the UnitPrice by the OrderQuantity for each row and summing the results to get the total revenue. Putting it all together, the formula calculates the total Revenues by summing the SalesAmount column and adding the result to the total revenue calculated by multiplying the UnitPrice by the OrderQuantity for each row in the Sales table.

ALL Revenues CALCULATE= CALCULATE( [Revenues], ALL ( Sales ) )

The formula explained

  • The CALCULATE function takes the [Revenues] measure as its first argument and applies a filter to the data.
  • The filter is specified by the ALL function, which removes any existing filters on the Sales table.
  • By removing all filters, the formula calculates the total revenue generated by all sales data, regardless of any other filters that may have been applied elsewhere in the report. In this case CALCULATE is removing the filter of Color beacuse there is written “ ALL ( Sales ) ”. So, not only calculate can introduce new filters, but can also remove the ones that already exist. We could find the same results applying the following formula: ALL REVENUES= SUMX( ALL( Sales ), Sales[UnitPrice]*Sales[OrderQuantity] ) Generate a formula that uses the CALCULATE function to calculate the revenues made by female customers. It must show the same number in all the rows. ✅Color ✅Revenues ✅ Revenues to F Cust CALCULATE ✅ ALL Revenues CALCULATE ALL Revenues to F Cust CALCULATE= CALCULATE( [Revenues], ALL ( Sales ), Customer[Gender]=”F” )

The formula explained

In this case we removed all the filters with “ ALL ( Sales ) ”, but we added a new one: Customer[Gender]=”F ”.

CALCULATE multiple OR Transform the following formula into a CALCULATE formula. Revenues to F Customers OR in 2001 OR to Bachelors= SUMX ( FILTER( Sales, RELATED(Customer[Gender]=”F” || RELATED(,Calendar’[CalendarYear])=2001 || RELATED(Customer[EnglishEducation])=”Bachelors” ), Sales[UnitPrice]*Sales[OrderQuantity] ) ✅Color ✅Revenues We cannot turn it into CALCULATE because we don’t know how to make an OR between columns of different tables. Calculate the total revenue earned from sales to female customers OR customers with a bachelor's degree (with SUMX and CALCULATE). ✅Color ✅Revenues SUMX Revenues to F Cust OR Bachelors= SUMX( FILTER(

Sales, RELATED(Customer[Gender])=”F” || RELATED(Customer[EnglishEducation])=”Bachelors” ), Sales[UnitPrice]*Sales[OrderQuantity] ) CALCULATE Revenues F Cust OR Bachelors CALCULATE= CALCULATE( [Revenues], Customer[Gender]=”F” || Customer[EnglishEducation])=”Bachelors” )

The formula explained

In the CALCULATE version the two filters Customer[Gender]=”F” and Customer[EnglishEducation])=”Bachelors” must be in the same row. We can also add other filters (columns) if they’re from the same Customer table.

Rename Revenues and call it Revenue (Orders). ✅CalendarYear ✅Revenues Create a new relationship between Date and Ship Date, keeping active the relationship between Date and Order Date.

Process explained

  1. Go to Model view
  2. Expand Sales
  3. Search “Ship Date”
  4. Drag “Ship Date” over “Date” (you find it in Calendar)
  5. You’ll see that the relationship has a dotted line since it is inactive You cannot have the two relationships Date – Order Date and Date – Ship Date active at the same time.

Generate a formula that calculate the total revenue based on the ship date of orders. Remember to make active the relationship between Date and Ship Date. ✅CalendarYear ✅Revenues (Orders) Shippings= CALCULATE( [Revenues (Orders)], USERELATIONSHIP( Sales[Ship Date], ‘Calendar’[Date] ) )

The formula explained

  • The USERELATIONSHIP function is used to specify the relationship between the Sales[Ship Date ] column and the ‘Calendar’[Date] column.
  • Once the relationship is established, the [Revenues (Orders)] measure is evaluated in the context of the Ship Date column from the Sales table and the Date column from the Calendar table. Calculate the book-to-ship ratio, which is a measure of how efficiently a company is fulfilling its orders. ✅CalendarYear ✅Revenues (Orders) ✅Orders Book to Ship= DIVIDE( [Revenues (Orders)] [Shippings] )

The formula explained

  • The formula starts with the DIVIDE function. This function is used to divide two values and handle any potential errors that may occur, such as dividing by zero.
  • The result is the book-to-ship ratio, which is the total revenues for all orders divided by the total shipping revenues for a particular date range. This ratio is often used to measure how quickly a company is able to fulfill its orders and can provide insight into the efficiency of its supply chain.