To refer to actions which had been in progress in the past.
Generally, those actions which had been in progress (and which are written in Past Perfect Continuous), were interrupted by another action (which is written in Past Simple).
This tense is used to talk about situations which were in progress during a period of time, so we generally use these prepositions
For - Since.
Subject + Had + Been + Verb With -Ing at The End + the rest of the sentence.
In order to write affirmative sentences, we write the auxiliary "Had" after the subject and followed by the word "Been". Then, we write the main verb with the -Ing form at the end of it (that indicates that the action was in progress).
I had been studying for hours when I fell asleep.
(The action in progress "I had been studying for hours" was interrupted when he fell asleep, written in the Past Tense).
They had been waiting for several minutes when the bus appeared.
Tom bought the house that he had been dreaming about since he was a child.
Subject + Had Not + Been + Main Verb with Ing in the End + the rest of the sentence.
The structure in the negative form is the same as in the Affirmative one. Add "Not" to the auxiliary verb, so it is written
Had Not (Hadn't).
They had not been living together for long when they broke up.
He hadn't been playing long when it started raining.
When the teacher came in, the children had not been speaking loudly.
Had + Subject + Been + Verb with Ing in the End + the rest of the sentence?
In the interrogative form, the auxiliary "Had" is written before the subject.
Had you been studying for a long time before the competition?
Had she been working for a long time at the office when she was fired?
Had he been running too much when he started feeling sick?