Open In App

How to Create a Relative File Path with Power Query in Excel?

Last Updated: 24 Jun, 2024

G

Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In today’s digital environment, sharing Excel files effectively is vital. A common issue is that Excel’s Power Query uses absolute file paths, which can break when files are opened on different systems.

To enhance portability and maintain data connections, it’s crucial to convert these absolute paths to relative paths. This article explains how to make Excel workbooks more shareable by using Power Query to switch from absolute to relative file paths.

How to Create a Relative File Path with Power Query in Excel?

How to Create a Relative File Path with Power Query in Excel?

Why Convert Absolute Paths to Relative Paths?

Problem with Absolute Path: Absolute paths can cause issues when sharing files because the path is specific to the original user’s computer. Due, to this file sharing, is a big and significant issue.

For example, an analyst is analyzing data in its intelligence tools source via power query from your PC, the tools work well until the files are in your PC, but stop working as soon as they are imported into the third person PC, this happens because the absolute path in the third person computer might be different, and our power query files are still searching for the old absolute path provided in my computer. To resolve this issue, one needs to convert this absolute path to the relative path, so that the files can access irrespective of one’s PC.

Excel Functions Used in Converting Absolute Path to Relative Path 

Before moving forward, we need to have a crisp knowledge of all the excel functions, that will be used to convert an Absolute path to a Relative path. There are broadly three functions used: =CELL(), =LEFT(), =FIND(), for creating the required formula: 

1. Cell Function 

Syntax: =CELL(info_type, reference)

The cell function provides every information, you require, for a cell. One can get the value of a cell, its row number, address, filepath, etc. This topic could be quite big itself, but for converting absolute path to relative path, we only need to know about how to get the file path of a cell. There are two arguments in cell functions: 

  • Argument 1:Info_type’ is the first argument of cell function. The type of information you want to find for the specified cell. For example, “filepath”, provides the absolute path for the current cell. 
  • Argument 2: ‘reference’ i.e., for which cell, do you want to extract the information. The cell reference can be absolute or relative. 

Note: The Excel file should be saved in some folder, then only the absolute path would appear, otherwise it will show an empty string returned. This is one of the common errors that users face while working with the =CELL(info_type, reference) function. 

For example, find the absolute path of the current opened excel file. Following are the steps: 

Step 1: Type =CELL(“filename”, $A$1), in cell B2, where “filename” provides the absolute reference of the file, and $A$1 is the reference to cell A1

Providing absolute reference of file

 

Step 2: Press Enter. The absolute Path of the current excel file appears in cell B2 i.e.

D:\geeksforgeeks\folder1\folder2\[geeksample.xlsx]Sheet1
Press Enter

 

Step 3: The path before the bracket is the absolute path for that Excel file. The text inside the square brackets is the name of the workbook, and at last, is the name of the worksheet. 

details of excel file

 

2. Find Function 

Syntax: =FIND(find_text, within_text, [start_num])

The function finds the first starting index of the location of a finding text in the given string. The indexing is 1-based. For example, if you are given a string “geeksforgeeks”, and you want to find the position of “ks” in your given string, then the answer returned by the =FIND() function will be 4. There are three arguments in the find function, but for converting the absolute path to a relative path, we will require only the first two arguments. 

  • Argument 1:Find_text’ is the first argument of the find function. The find_text is the string that needs to be found in the given string. The first occurrence of the find_text is printed using the =FIND() function. 
  • Argument 2: ‘Within_text’ is the second argument of the find function. The within_text is the original string in which find_text is searched. 
  • Argument 3: ‘start_num‘ ,This is an optional argument. It tells from which index you should start the search in the within_text. 

For example, you recently found the absolute path for the current excel file, our task is to find the index of “[” (square bracket) in the given string i.e. “D:\geeksforgeeks\folder1\folder2\[geeksample.xlsx]Sheet1”. Following are the steps: 

Step 1: Type

=FIND("[", "D:\geeksforgeeks\folder1\folder2\[geeksample.xlsx]Sheet1"), 

In the cell B2, where Argument1 is “[“ and Argument2 is “D:\geeksforgeeks\folder1\folder2\[geeksample.xlsx]Sheet1”. Press Enter

Typing function in B2

 

Step 2: We can see 34 appear in cell B2. This is because the “[“ appears at the 34th (1-based indexing) index in the original string. 

34 appears in B2

 

3. Left Function 

Syntax: =LEFT(text, [num_chars])

The function returns the prefix substring of a string, according to the user-specified number of characters. The indexing is 1-based. For example, if you are given a string “geeksforgeeks”, and you want to find the first four(4) characters in the given string, then we can use the =LEFT() function, to achieve this, the function will return a prefix substring “geek”. There are two arguments in the left function. 

  • Argument 1: ‘text’; The first argument is the text string. The text string is the string for which the prefix substring has to be returned. 
  • Argument 2: ‘num_chars’; The second argument is num_chars. The num_chars is the number of characters you want from the starting of the text string. 

For example, you recently found the absolute path of the current worksheet, and you also found the index of the square bracket “[” i.e. 34, our task is to find the prefix substring before the “[” (square bracket) which means the number of character to be 33. Following are the steps: 

Step 1: Type =LEFT(“D:\geeksforgeeks\folder1\folder2\[geeksample.xlsx]Sheet1”, 33), in the cell B2, where argument1 is the original string, for which prefix substring has to be returned, and the second argument is the number of characters for which this given string has to be returned. 

Typing function in B2

 

Step 2: Press Enter. All first 33 characters will appear in cell B2 i.e.

 "D:\geeksforgeeks\folder1\folder2\"
33 character appears in B2

 

Converting Absolute Path to Relative Path in Power Query

Now, you know everything to convert an absolute path to a relative path, in a power query. We will take the same example and file location which we did, for understanding the functions. The absolute path for the current file is “D:\geeksforgeeks\folder1\folder2\housing.csv”. This needs to be converted into the relative path. Following are the steps: 

Step 1: As you are understanding an advanced topic of power query, so we will assume that you know how to get data from a CSV or xlsx file in power query. Imported a table, in the current sheet name “housing”, using Get Data

table imported

 

Step 2: Now, on the right side of your screen, you will have the housing data set, that you imported. Double-click on it and the power query editor will be launched. The main aim for step1 and step2 is to open our power query editor. 

housing data set available

 

Step 3: The power Query editor is opened. In the Home tab, under the Query section, click on Advanced Editor

Clicking advanced editor

 

Step 4: The Advanced Editor is opened. You can observe that the M-code is written in the picture shown below, and the absolute path for the current CSV file is  “D:\geeksforgeeks\folder1\folder2\housing.csv”. Our task is to convert this absolute path into a relative path. 

converting-absolute-path-into-relative-path

 

Step 5: Close all the windows, and return back to the “housing” worksheet. Now, create a new worksheet, named “setUp”. Cell B2 has the value FilePath

filepath setup

 

Step 6: Now, comes the most important step. Now, if you have understood the examples provided while explaining the different functions like =CELL(), =FIND(), =LEFT(), then this will be a very easy step for you. The formula written is:

=LEFT(CELL("filename", $A$1), FIND("[", CELL("filename", $A$1)) - 1).

The formula simply extracts the text written before the square bracket “[“, in the absolute path, i.e. from “D:\geeksforgeeks\folder1\folder2\[geeksample.xlsx]setUp” to “D:\geeksforgeeks\folder1\folder2\”

File path formula

 

Step 7:  Press Enter. “D:\geeksforgeeks\folder1\folder2\” appears in the cell C2

Entering file path in C2

 

Step 8: Change the name of the cell, C3 to Filepath

Changing C3 to filepath

 

Step 9: Now, repeat steps 1, 2, and 3. This will open our advance editor again. Now, add this single line of M-code in your advanced editor,

"Filepath = Excel.CurrentWorkbook(){[Name="Filepath"]}[Content]{0}[Column1],". 

This code simply stores the reference of the cell C3(Filepath) in the variable “Filepath”

Storing reference of the cell

 

Step 10: Now, come to the second line of M-code, where some File content is stored in the variable “Source”. Inside the File.Contents function, change “D:\geeksforgeeks\folder1\folder2\housing.csv” to ” Filepath & “housing.csv” “.

Close all the windows, and return back to the “housing” worksheet, this converts your absolute path to the relative path of the current added data in your worksheet. 

absolute-path-converted-to-relative-path

Conclusion

In conclusion, Creating relative file paths in Power Query for Excel is a vital skill for anyone who frequently shares Excel workbooks or works in collaborative environments. By converting absolute paths to relative paths, you make your Excel files more shareable and functional across different computers. Using the CELL, FIND, and LEFT functions in conjunction with Power Query, you can achieve this easily.

How to Create a Relative File Path with Power Query in Excel? – FAQs

What is a relative file path in Excel?

A relative file path in Excel specifies the location of a file relative to the current directory, making it easier to share and open the file on different computers without breaking data connections.

Why should I use relative paths instead of absolute paths in Excel?

Using relative paths ensures that your data connections remain intact regardless of where the file is accessed, enhancing the portability and shareability of your Excel workbooks.

How does Power Query handle file paths by default?

By default, Power Query uses absolute paths, which can cause issues when the file is shared or moved to a different directory or computer.

How do I convert an absolute path to a relative path in Power Query?

To convert an absolute path to a relative path in Power Query, you need to use Excel functions like CELL(), LEFT(), and FIND() to extract and manipulate the file path, and then update the M-code in the Power Query Advanced Editor.

What are the steps to create a relative path in Power Query?

Steps to create a relative path in Power Query are:

  • Step 1: Open Power Query Editor and import your data.
  • Step 2:Use the CELL, FIND, and LEFT functions to extract the directory path.
  • Step 3:Update the Power Query M-code to use the extracted relative path.

What Excel functions are essential for converting absolute paths to relative paths?

The essential Excel functions are CELL(), LEFT(), and FIND(), which help in extracting and manipulating the file path to create a relative path.



G

News
Improve
Discuss
Do you want to advertise with us?Click here to know more

Power Query – Source Reference as File Path in Cell

article_img
Power query helps in doing automation in an efficient manner. It allows users to utilize files stored in specific locations and apply routine transformation steps on those files. It allows users to embed file paths and file sources in an Excel cell. The end user can make use of named ranges and Excel cells as references to access multiple files. In this article, we will learn how to refer to Source as a file path in a cell.  Adding Source Reference as File Path in Cell Let us take a basic example of how to make use of Excel cells in manipulating file paths. Below is an example where 2 Excel files are there, Power query1.xlsx and Power query2.xlsx.  The reference to source as file path, is only valid if the excel files, have same table name, and same number of columns in it. For example, Power query1.xlsx and Power query2.xlsx, has same table name(Table_src) and same number of columns.  Step 1: In the above example, Excel cells B2 and B3 contain the path of the Excel file. Let us bring the data to be used for transformation. The name of cell B2, should be replaced with FileP, as shown in the image below. FileP will be the name range cell of B2.  Step 2: To do this, go to the Data ri
Read More

Excel - Types of Merging of Queries in Power Query

article_img
In Power Query, we have an option to Combine two tables using “Merge queries”. Merge Queries: Merge with current query with another queryMerge Queries as New: Merge with current query with another query and create New query In this article, we explore different types of merging of queries in Power Query with two tables. We will combine two years of sales data (2020 and 2021) with the key field “Cust_ID”. Implementation: Follow the below steps to implement merging of queries: Step 1: Open Power Query Editor. Step 2: Select your Query “Sales_2020”. Go to Home >> Merge Queries >> Merge Queries as New. We have six type of joins: Left Outer (all from first, matching from second)Right Outer (all from second, matching from first)Full Outer (all rows from both)Inner (Only Matching rows)Left Anti (rows only in first)Right Anti (rows only in second) Step 3: Select Second table; Select key fields “Cust_ID” in both tables; Select Left Outer (all from first, matching from second) and Press “OK”. Step 4: Above step create a new query “Merge 1” as below in your power query. Note: Sales_2021 is not a text value it is a table for each row. Step 5: Click “Sales_2021” drop down icon; M
Read More

C

Date Modifications with Power Query in Excel

A data preparation and transformation engine is Power Query. Power Query includes a Power Query Editor for implementing transformations as well as a graphical interface for obtaining data from sources. With Power Query (also referred to as Get & Transform in Excel), you may import or connect to external data and then modify that data to match your needs, for instance, by combining tables, altering the data type, or eliminating a column. When you're ready to produce charts and reports, load your query into Excel. You may periodically update the data by refreshing it. Steps to Open Power Query in Excel Step 1: First create a table in Excel. Step 2: Select the field and then navigate to the data tab on the top of the ribbon. Then select the Get data option and choose From other sources and select From Table/Range option. Step 3: Then Create Table dialog box appears. Here select the table range and then click OK. Step 4: Now you can see it converts the excel table to a power query Editor. Date modification with Power QueryAge The number of days between the specified date and the current date is determined when you choose the Age option and choose a date column. Next, you mu
Read More

Text Modifications with Power Query in Excel

article_img
In Excel, Power Query is a business intelligence tool that is used to analyze the data imported from different sources. With the help of power query, we can easily clean, transform and reshape our data as per requirement along the way of analyzing the data. In this example, we will learn about the Text Functions which can be easily used with the help of Power BI for the modification of the data. Given below are the formatting functions that can be used to create and manipulate the text values. Function Name Description Trim It is used to trim(remove) the whitespace from both ends of the text.Clean It removes the non-printable characters from the text and returns it.Merge Columns It is used to combine two or more columns into a single column using a separator.Lowercase It is used to make text lowercase.Uppercase It is used to make text uppercase.Add Prefix It is used to add prefixes to a cell or set of cells.Add Suffix It is used to add suffixes to a cell or set of cells.Capitalize Each Word It is used to change a text into proper text.Text Modifications with Power Query Step 1: Create a Database. In order to use the text operations, we will be using the following random names.  Fig
Read More

C

How to Add a Conditional Column in Power Query in Excel?

We may use Power Query to generate new columns whose values depend on one or more criteria that have been applied to existing columns in your database. The condition imposed on an existing column in the data model serves as the foundation for Power BI's conditional column. On the Add column tab, in the General category, is the Conditional column command. In this article, we are going to discuss the Conditional column in Power Query and in Power Pivot queries. Step By Step Implementation Step 1: Create an excel table with fields Roll No., Student name, and Marks. Step 2: Select a table. Then navigate to the data tab on the top of the ribbon and select the Get Data option and then go to from other sources and choose from Table/range. Step 3: Then create a Table dialog box appear. Here select the range and check my table has headers then click on OK. Step 4: Now as you can see the excel table is converted to a power query editor. Step 5: Now in the power query go to the add column tab on the top of the ribbon and here select the conditional column. Step 6: Add Conditional Column dialog box appears. Here write the new column name and then apply the if condition. Step 7: Her
Read More

D

How to Import, Edit, Load and Consolidate Data in Excel Power Query?

article_img
Power Query is an easy and efficient way of solving simple data tasks. Most of our valuable time is frequently consumed by tedious manual procedures like cut and paste, column merging, and filtering. These operations are greatly simplified with the Power Query tool. A further advantage is that, in comparison to other BI tools, Power Query is simple to use. The Power Query user interface is straightforward. Given how much it resembles the Excel interface, many users will find it to be familiar. What is Power Query? Power Query is an application for preparing and transforming data. Using Power Query, you may perform transformations to data obtained from sources using a Power Query Editor and a graphical user interface.  We can import data from a variety of sources, clean it, convert it, and then reshape it to suit our needs using Power Query, a business intelligence tool provided by Microsoft Excel. Power Query allows us to create a query just once and reuse it later by simply refreshing it. Different Ways to Import Data to the Excel Sheet Excel allows importing data from a variety of sources. We will look at each of these in detail. While importing data, power query has several feat
Read More

Relative References in Excel Macros

article_img
We have two options to refer a cell in Excel VBA Absolute references and Relative references.  Default Excel records macro in Absolute mode. In this article, we learn about Relative references in Excel VBA.  We select a cell “A1”, turn on “Use Relative Reference” and record a macro to type some text in cells B2:B4.   Since we turn on the “Relative reference” option.  Macro considers the number of rows and columns from active cells.  In our example, we select cell A1 and start type B2, which is to move one column and one row from A1 (Active cell). What are Relative References?In Excel, the way of recording actions within a macro that is relative to the cell or range that the macro starts from is known as a relative reference. When you start recording a macro with relative reference, Excel records the actions based on the position of the active cell at the beginning of the macro. The recorded actions will adjust accordingly when the macro is run on different ranges. Using Relative ReferencesWe must make sure that the macro is recorded starting from the cell where the steps must begin in order to record a macro with relative references. ImplementationFollow the below steps to implemen
Read More

How to Calculate Relative Standard Deviation in Excel?

article_img
Definition: Related standard deviation is also known as the relative percentage standard deviation form, the deviation measurement which tells us how different numbers are dispersed around the mean in a particular set of data. This format shows the percentage distribution of data. If a relative standard deviation of the product is higher, that means that the numbers are very wide-ranging from the mean of that product. The RSD team sometimes needs certain data which is far removed from the Average RSD according to product requirement. Data that are well deviated from the RSD are taken into consideration in those cases. In the event of the reverse situation, i.e. a lower relative default, the numbers are closer than the average and are also referred to as the coefficient of difference. In general, the idea of actual forecasts is given within the given data set. Uses of Relative Standard Deviation:Relative standard deviation is used widely in the interpretation of statistical data relations in different segments.This is one of the primary tools indicating whether the stock price is changing the growth of the company.RSD is a sophisticated version of an analytical tool that assists the
Read More

Relative, Absolute and Mixed Cell Reference in Excel

article_img
Understanding how cell references in Excel work is a cornerstone of effective spreadsheet use. Whether you’re building formulas that auto-adjust to changes, locking a specific cell, or creating dynamic worksheets, the types of cell references—relative, absolute, and mixed—play a vital role. This guide will break down each type of cell reference, show you how to switch between them using tools like the Excel F4 shortcut and provide practical examples to help you apply them effectively in your formulas. By the end, you’ll have a clear understanding of how these references enhance accuracy and flexibility in your Excel tasks.Table of ContentWhat is a Cell Reference in ExcelTypes of Cell Reference in Excel1. Relative Cell Reference 2. Absolute Cell Reference 3. Mixed Cell Reference Other Useful Ways to Use Cell References and AddressesHow to Switch between Different Cell ReferencesCell References Across WorksheetsWhat is a Cell Reference in ExcelA cell reference (or cell address) is a combination of a column letter and a row number that identifies a specific cell in a worksheet. These references are essential for formulas and functions, allowing you to retrieve, calculate, or manipulat
Read More

Excel Relative and Absolute Cell References

article_img
When working with formulas in Excel, it's crucial to understand the difference between relative and absolute cell references. These two types of cell references behave differently when copied or filled into other cells. Relative cell references change based on their position when a formula is copied to another cell, making them highly useful for repetitive calculations across different rows or columns. Absolute cell references, on the other hand, remain constant regardless of where they are copied, ensuring that a specific cell reference does not change. By mastering both relative and absolute references, you can enhance your Excel skills, making your data management and formula application more efficient. Whether you're a beginner or an experienced user, knowing how to use these references correctly is essential for accurate and effective spreadsheet work. Table of Content What is a Cell Reference in Excel?Types of Cell References in ExcelRelative Reference in ExcelAbsolute Reference in ExcelHow to Use Excel's Relative Reference - Example How to Use Absolute Reference: ExampleHow to use Cell References with Multiple Worksheets in ExcelDifference Between Absolute and Relative cell
Read More
three90RightbarBannerImg