Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

DA 2.5: Blending a sheet, a file and a table

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

Three sources, three shapes

Sales arrive as a spreadsheet export, a file from the website and a table from the till system.

Each writes dates and numbers its own way. To report on all three, you must blend them into one final format.

Key words

WordWhat it means
Dataseta collection of related data about one subject
Blendingbringing data from different sources into one dataset
Formatthe way data is stored, such as a sheet, JSON or a table
Data typethe kind of value a column holds
JSONa text format of nested records with braces and quotes
Castchange a value from one type to another
Final formatthe shape the blended data must have at the end

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can blend datasets from different formats into one final format.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Identify and analyse each dataset

List each dataset with its format, its columns and the data type of each. Note the oddities: "N/A" in a number column, dates written day first, names in capitals.

This list is your map for the next step.

The three datasets side by side

SourceFormatunits arrives asDate arrives as
Spreadsheet exportText values from a sheetText, may say N/A3 April 2026, day first
WebsiteJSON fileNumber or text2026-04-05
Till systemTableWhole numberDate

The cells students fill in are left blank in this preview.

Prepare and standardise each one

Check consistency of data types: every units column must be a whole number. Check compatibility of formats: every date must be written the same way.

Use a cast to change type. Where the cast cannot work, such as "N/A", the value becomes missing. Then decide what to do about it.

Try it: standardise the sheet export

Code: duckdb
select product,
       try_cast(units as integer) as units,
       strptime(sold_on, '%d/%m/%Y')::date as sold_on
from from_sheet;

-- Try it: which row has a missing units value now?

Try it: read the JSON text

Code: duckdb
select j->>'product' as product,
       try_cast(j->>'units' as integer) as units,
       (j->>'sold_on')::date as sold_on
from (select unnest(from_json('[{"product":"helmet","units":3,"sold_on":"2026-04-05"},{"product":"bell","units":"4","sold_on":"2026-04-05"}]', '["JSON"]')) as j);

-- The 4 arrived as text. The cast makes it a number.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.