In modern Excel, a single cell can contain multiple fields of data. This is possible thanks to data types in Excel. These data types are often referred to as rich data types to distinguish them from existing data types such as number, string, and Boolean.
We will begin this chapter with a short explanation of the different data types available in Excel. We then get down to the purpose of this book and learn how we can use formulas to access and perform calculations on data type fields.
This chapter is not intended to provide a complete understanding on using data types in Excel, but how we can access data within the data types using formulas.
These formulas will include popular requirements for Excel users such as returning live exchange rate data and performing currency conversions and retrieving historical stock market data. We will also see how we can use some of our favorite Excel functions such as SUM, FILTER, and XLOOKUP with data types.
data-types.xlsx
Introduction to Data Types in Excel
Availability : Excel for Microsoft 365, Excel for the Web, and Excel for Microsoft 365 for Mac
Data types are a real game changer in how we think about Excel. Being able to store many fields of data in a single cell is quite radical. These additional hidden fields of data can still be accessed using formulas in Excel while remaining hidden on the sheet.
There are a few different data types in Excel. These include data types that are connected to an online data source to pull in live data, such as currency exchange rates, and the ability to create your own custom data types using Power Query or Power BI.
The data types available to you are dependent upon a few factors including your version of Excel, what has been enabled by your IT administrators, and the language that you use for Excel.
This is a very new feature to Excel that is expanding fast with new data types and further language support becoming available.
Stocks: Return data related to types such as stock, equity, mutual fund, ETF, currency pairs, and commodity. For example, Microsoft Corp or Barclays PLC.
Currencies: Return live exchange rates. Enter the two currencies separated by a forward slash (/) or a colon (:). For example, USD/EUR or GBP:EUR.
Geography: Return data related to countries, cities, states, provinces, counties, and districts. For example, Canada or Ottawa.
Wolfram: Data powered by Wolfram to provide additional data about food, movies, yoga, terrain, chemistry, animals, and more. These data types are only available in Excel for Microsoft 365 Home/Student edition, so will not be covered in any examples.
Organization: A custom data type that is created in Power BI and published to the Power BI Service. These data types are then pulled into Excel using your organization account. For example, data related to an organization such as sales or inventory could be made a data type and then accessed in Excel as an organization data type.
Power Query: A custom data type that is created in Power Query in Excel. You can convert any data in Power Query into a data type and then load it to an Excel table.
Data Types information is updated by clicking Data ➤ Refresh All. Some data types such as Stocks allow you to specify the refresh interval such as every five minutes, while others can only be manually refreshed. The default refresh interval is set by the data provider for the data type.
Accessing the Data Type Fields with Formulas
Let’s get into the exciting stuff and write some formulas. We will see examples that extract and analyze data using the Geography, Currencies, Stock, and Power Query data types in this chapter. We will start with the Geography data type, but the examples shown apply to all data types.
Otherwise, you can convert values to a data type by selecting the range of values and clicking the data type you need on the Data tab of the Ribbon.
These data types can store many fields in a single cell. We can access the fields of a data type using formulas, without having to display the field on the sheet. However, let’s start by looking at how we can insert some chosen fields onto the grid of Excel.
The same list of fields can be accessed using formulas. In Figure 14-5, the formula to access the list of fields for a data type is shown. Typing a period (.) after the reference to a cell containing a data type shows the list of available fields.
You may be wondering where Excel is getting the data from. If you click the data type icon to the left of its name, a card is displayed. Scroll to the bottom of the card to see a list of sources that Excel uses to retrieve the data.
Further fields that you insert can themselves be a data type. In Figure 14-7, the [Capital/Major City] field has been added for each country. These cities are automatically recognized as Geography data types by Excel.
In Figure 14-8, the population for each [Capital City] has been returned with a simple formula. When referring to a data type, you refer to the cell in the same manner as we would if it contained a standard text or numeric value.
Aggregating the Values in a Data Type Field
Let’s move on to using functions with data type fields and begin with an aggregation function.
FIELDVALUE Function
Availability: Excel for Microsoft 365, Excel for the Web, and Excel for Microsoft 365 for Mac
The FIELDVALUE function is used to extract a value from a specified field of a data type. We have just seen examples of how to achieve this using the “.” operator following a cell that contains a data type, for example, B2.[Calling Code] or [@Country].Population. So, you might be thinking why we would use the FIELDVALUE function.
The simple formula to extract a field from a data type does not work in all scenarios. The most common of these scenarios is when performing conditional calculations.
We will see an example using the FIELDVALUE within an IF function now and within a SUM function to perform a conditional sum later in this chapter.
Value: A reference to the data type. It returns all fields from the referenced data type.
[field name]: Name or names of the fields from which to extract the value. The field name must be entered within double quotes (“ ”).
Using Functions with Data Type Data
Let’s now explore how some of our favorite Excel functions, such as XLOOKUP, SUM, and FILTER, can be used to extract and calculate data from these rich data types.
The technique for using formula and functions with data types applies for all kinds of data type. For the next few examples, we will demonstrate the functions with Power Query data types.
Although this book is about formulas, I feel it would be remiss of me to fully disregard a demonstration of how to create a Power Query data type. However, this will be brief, as it is not the focus of the chapter.
Creating a Custom Data Type with Power Query
- 1.
Click the [tblStores] table and click Data ➤ From Table/Range to load it into Power Query.
- 2.
Select the columns to be used in the data type.
- 3.
Click Transform ➤ Create Data Type (Figure 14-12).
- 4.
In the Create Data Type window, type a name for the data type in the Data type name box and specify the Display column from the list (Figure 14-13). Click OK.
- 5.
Close & Load to a table in Excel.
Figure 14-14 shows the table loaded to cell A1 of a sheet named [Stores]. The table is named [StoresData], and the [Store Name] field is displayed on the cell as that was specified as the Display column in the Create Data Type window (Figure 14-13). The data type icon is shown to the left of the store name.
XLOOKUP with Data Types
For the first function example, let’s see the XLOOKUP being used to search for and return a value from the data type based on a cell value.
Conditional SUM Function with Data Types
Now, let’s use the incredible SUM function to count and sum values of the data type based on conditions.
The FIELDVALUE function is used to return the last year’s revenue value for testing. An array for TRUE and FALSE values is returned as results for the conditional test. To sum these values, the double unary (--) is used to convert the TRUE and FALSE values to 1 and 0.
Using the SUM function with arrays as demonstrated in these examples was explained in Chapter 10. Visit that chapter if you are interested in understanding how these formulas work in more detail.
FILTER Function with Data Types
For the final example of using some of our favorite functions with data types, we will see the FILTER function .
The CHOOSE function is used to specify the return of the [Store Name], [Last Year Revenue], and [Top Sale Rep] fields only. FILTER returns the rows for the South region only. And SORT then orders the results in descending order by column 2, the [Last Year Revenue] column.
Currency Conversion Using Data Types
With the Currencies data type in Excel, you can return live currency data such as exchange rates. If you do not see a Currencies data type in your version of Excel, the Stocks data type can be used instead. Both data types pull their information from the Stocks source.
Figure 14-18 shows the exchange rate being returned for two currency pairs – US Dollars to Euros and Danish Krone to Euros.
The [Price] field is used to return the exchange rate. The [Last Trade Time] field can be used to display the date that the [Price] was last updated.
To create the Currencies or Stocks data type, simply enter the two ISO currency codes separated by a forward slash (EUR/GBP), a colon (EUR:GBP), or by nothing at all (EURGBP). Select the cells containing the currency pairs and click Data ➤ Currencies or Data ➤ Stocks.
You can return the ISO currency code for a country using the [Currency Code] field of the Geography data type.
- 1.
Right-click a cell containing the data type.
- 2.
Click Data Type ➤ Refresh Settings.
- 3.
Click the Stocks data type in the Data Types Refresh Settings pane to expand the refresh settings (Figure 14-20).
- 4.
Click the refresh option you want to use from Automatically every 5 minutes, On file open, or Manually.
Historical Stock Data with STOCKHISTORY
Availability : Excel for Microsoft 365, Excel for the Web, and Excel for Microsoft 365 for Mac
The STOCKHISTORY function returns historical stock price and currency rates based on a given symbol and date range. It can return a dynamic array of results from a single formula.
Stock: Reference to a cell containing the Stocks data type or the ticker symbol entered within double quotes (e.g., “MSFT”). Data is pulled from the default exchange; however, you can specify the exchange by entering the four-character ISO market identifier followed by a colon (:) before the ticker symbol (e.g., XNAS:MSFT).
Start date: The earliest date for the data to be returned. The date can be entered as a fixed date enclosed in double quotes (e.g., “01/04/2022”), a formula (e.g., TODAY()-10), or a reference to a cell containing a date value.
[End date]: The latest date for which to retrieve stock data. This argument is optional, and when omitted the start date is used.
[Interval]: Specify the interval that each value represents. Type 0 for daily, 1 for weekly, or 2 for monthly. If omitted, a daily interval is used.
[Headers]: Specify whether headers should be displayed. Enter 0 for no headers, 1 for with headers, or 2 for headers prefixed with the instrument identifier. The default is to display the headers.
[Properties]: The columns to be returned for each stock. The properties are specified by the following index numbers: 0 = Date, 1 = Close, 2 = Open, 3 = High, 4 = Low, 5 = Volume. The columns/properties are returned in the order provided, for example, 0, 2, 1. The default is Date and Close (i.e., 0, 1).
Date: The first valid trading day in the period
Close: The closing price on the last trading day in the period
Open: The opening price on the last trading day in the period
High: Highest price of the highest day’s high in the period
Low: Lowest price of the lowest day’s low in the period
Volume: The volume traded during the period
When using an interval other than daily, the start date will revert to the first date of the specified period, so may be earlier than the stated start date.
Simple STOCKHISTORY Example
Let’s start with a simple STOCKHISTORY example that pulls stock prices for a specified stock in the London Stock Exchange.
The default values are used for the interval, headers, and properties arguments. So, the interval is set as daily, headers are inserted as part of the array, and the default properties of Date and Close are used.
Adding Properties to STOCKHISTORY
STOCKHISTORY provides six properties that can be returned by the function. These properties are entered in the order that you would like them to be returned.
Inserting a Stock Chart Using STOCKHISTORY Data
The data returned by STOCKHISTORY can be presented in a chart for a greater visual of the stock trend over time.
When using STOCKHISTORY in its default manner, which returns the Date and Close price data only, a line chart would be great. But to play around more with STOCKHISTORY, let’s insert one of the stock charts available in Excel.
Excel provides four different stock charts natively (there are chart techniques to create your own variations). We will insert the Open-High-Low-Close stock chart.
It returns the properties of Date, Open, High, Low, Close (0,2,3,4,1) in that order, as requested by the stock chart description.
- 1.
Select range to be used for the chart data.
- 2.
Click Insert ➤ Insert Waterfall, Funnel, Stock, Surface, or Radar Chart button ➤ Open-High-Low-Close.
The chart is inserted and can be refined further as desired. Figure 14-25 shows the Open-High-Low-Close stock chart inserted in this example. The chart title has been linked to the value in cell B2.
Returning Historical Exchange Rates with STOCKHISTORY
As we saw earlier in this chapter, exchange rate data comes from the Stocks data type, even though a Currencies type is provided for simplicity. The STOCKHISTORY function can return historical exchange rate data just as it does for stocks.
For a demonstration of this, we will return historical exchange rate data for multiple currency pairs and for a specified five-day period.
In this example, the headers are disabled, as they are not required. And a single property of the close price is requested.
Summary
In this chapter, we learned how to use formulas to access and analyze data stored in rich data types.
The chapter began with an introduction to what data types are and the different data types available in Excel. We then saw how to use some popular functions such as SUM, FILTER, and IF to work with data types. We also covered a couple of new functions – FIELDVALUE and STOCKHISTORY.
Data types are a new feature of Excel, only a few years old, and it is exciting to see how they grow and mature as a tool.
In the next chapter, we will learn lots of new Excel functions – LET, LAMBDA, and the seven LAMBDA helper functions.
The LET function is a fantastic addition that makes complex formulas more efficient. It improves their calculation speed and readability.
With LAMBDA, we can create our own custom functions. This is so much fun. We now have the tools to create the functions that we want from within the Excel experience. These functions can then be distributed to others.