Садржај

Sometimes there are situations when it is not known in advance exactly how many and which rows need to be imported from the source data. Suppose we have to load data from a text file into Power Query, which, at first glance, does not present a big problem. The difficulty is that the file is regularly updated, and tomorrow it may have a different number of lines with data, a header of three, not two lines, etc.:

Увоз плутајућег фрагмента у Повер Куери

That is, we cannot say in advance with certainty, starting from which line and exactly how many lines need to be imported. And this is a problem, because these parameters are hard-coded in the M-code of the request. And if you make a request for the first file (importing 5 lines starting from the 4th), then it will no longer work correctly with the second one.

It would be great if our query could itself determine the beginning and end of the “floating” text block for import.

The solution I want to propose is based on the idea that our data contains some keywords or values ​​that can be used as markers (features) of the beginning and end of the data block we need. In our example, the beginning will be a line starting with the word Шифра, and the end is a line with the word укупан. This row validation is easy to implement in Power Query using a conditional column – an analogue of the function IF (АКО) в Microsoft Excel.

Let’s see how to do it.

First, let’s load the contents of our text file into Power Query in the standard way – through the command Data – Get data – From file – From text/CSV file (Data – Get Data – From file – From text/CSV file). If you have Power Query installed as a separate add-in, then the corresponding commands will be on the tab Повер Куери:

Увоз плутајућег фрагмента у Повер Куери

As always, when importing, you can select the column separator character (in our case, this is a tab), and after importing, you can remove the automatically added step модификованог типа (промењен тип), because it’s too early for us to assign data types to columns:

Увоз плутајућег фрагмента у Повер Куери

Now with the command Adding a Column – Conditional Column (Add Column — Conditional Column)let’s add a column with checking two conditions – at the beginning and end of the block – and displaying any different values ​​​​in each case (for example, numbers 1 и 2). If none of the conditions is met, then output нула:

Увоз плутајућег фрагмента у Повер Куери

После клика на OK we get the following picture:

Увоз плутајућег фрагмента у Повер Куери

Now let’s go to the tab. Трансформација и изаберите тим Попунити доле (Transform – Fill – Down) – our ones and twos will stretch down the column:

Увоз плутајућег фрагмента у Повер Куери

Well, then, as you might guess, you can simply filter the units in the conditional column – and here is our coveted piece of data:

Увоз плутајућег фрагмента у Повер Куери

All that remains is to raise the first line to the header with the command Користите први ред као заглавља табулатор Početna (Home – Use First Row as Headers) and remove the unnecessary more conditional column by right-clicking on its header and selecting the command Избриши колону (Delete Column):

Problem solved. Now, when changing the data in the source text file, the query will now independently determine the beginning and end of the “floating” fragment of the data we need and import the correct number of lines each time. Of course, this approach also works in the case of importing XLSX, not TXT files, as well as when importing all files from a folder at once with the command Подаци – Преузми податке – Из датотеке – Из фасцикле (Подаци — Узми податке — Из датотеке — Из фолдера).

  • Склапање табела из различитих датотека користећи Повер Куери
  • Redesigning a crosstab to flat with macros and Power Query
  • Building a Project Gantt Chart in Power Query

Ostavite komentar